插入触发器不工作后

时间:2013-10-15 20:54:33

标签: sql-server triggers insertafter

我正在尝试运行一个插入后触发器,但它不起作用,有没有办法查看它的日志?

这是我的代码

ALTER TRIGGER [dbo].[SUBSID_INSERT] on [dbo].[STUDENT_MASTER] AFTER INSERT AS
BEGIN 
SET NOCOUNT ON
DECLARE @numrows int, @hostname char(30), @sysuser char(30);

SELECT @numrows = @@rowcount;
IF @numrows = 0 RETURN;

SELECT @hostname = substring(hostname, 1, 30)
FROM master.dbo.sysprocesses
WHERE spid = @@spid;

SELECT @sysuser = substring(system_user, 1, 30);

INSERT INTO insert statement,  


FROM STUDENT_MASTER JOIN INSERTED I ON STUDENT_MASTER.ID_NUM = I.ID_NUM
JOIN NAME_MASTER ON NAME_MASTER.ID_NUM = STUDENT_MASTER.ID_NUM


WHERE 
STUDENT_MASTER.id_num not in ( select DISTINCT id_num from subsid_master where subsid_cde LIKE '0%')

END

GO

1 个答案:

答案 0 :(得分:4)

您在声明变量后正在检查@@ROWCOUNT。这将始终显示最后一个语句的结果(在本例中为0),因此请停止这样做。而只是使用:

SELECT @numrows = COUNT(*) FROM inserted;

我看到了许多其他潜在的改进,但这正是它目前似乎“无法正常工作”的原因。