sql server 2008 触发器的问题
我在A表写了个触发器 插入后执行,是向B表插入一条记录
USE [test]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[tba_delete]
ON [dbo].[tba]
AFTER INSERT
AS
BEGIN
SET NOCOUNT ON;
insert into test.dbo.tbb(title,info) values('title','info');
END
insert into test.dbo.tbb(id,title,info) values(1,'title','info');
USE [test]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create table [tba](title varchar(20),info varchar(20))
create table tbb(title varchar(20),info varchar(20))
--创建一个存放错误信息的表
create table tb_error_message
(
obj_name nvarchar(30),
obj_type nvarchar(15),
err_msg nvarchar(100),
err_date datetime
)
go
drop trigger tba_delete
go
create TRIGGER [dbo].[tba_delete]
ON [dbo].[tba]
AFTER INSERT
AS
BEGIN
SET NOCOUNT ON;
begin try
--这里模拟插入title列的数据长度,超过了定义时的20
insert into test.dbo.tbb(title,info) values('title1111111111111111111','info');
end try
begin catch
--错误了就回滚
rollback;
insert into tb_error_message
values('tba_delete','trigger',error_message(),getdate())
end catch
END
go
insert into tba
values('11','11')
/*
消息 3930,级别 16,状态 1,过程 tba_delete,第 16 行
当前事务无法提交,而且无法支持写入日志文件的操作。请回滚该事务。
语句已终止。
*/
--查询出错的信息
select *
from tb_error_message
/*
obj_nameobj_typeerr_msgerr_date
tba_deletetrigger将截断字符串或二进制数据。2013-11-08 12:32:16.750
*/