在MS SQL Server中的表上,如果这些行满足特定条件,我需要对新行运行CLR存储过程。我认为这或多或少是直截了当的:
if ((SELECT Cabinet FROM INSERTED) = 1 OR (SELECT Cabinet FROM INSERTED) = 3) AND (SELECT Result_02 FROM INSERTED) = 'N'
BEGIN
DECLARE @id int
SET @id = (SELECT ID FROM INSERTED)
exec PrintLabel
@id,
N'LP 2844 LGE'
END;
但是,最近开始抛出有关返回多行的子查询的错误。我了解到INSERT触发器对每个语句运行一次,不一定在各行上运行。因此,IF语句可以处理多行,从而导致错误。
我的问题是:如果批量添加行(在这种情况下通过MERGE语句),我如何迭代每个新行。
答案 0 :(得分:1)
为N行调用存储过程仍然需要游标。
declare @id int
declare cur cursor fast_forward for
select id
from inserted
where Cabinet in (1,3)
open cur
fetch next from cur into @id
while @@fetch_status = 0
begin
exec PrintLabel @id, N'LP 2844 LGE'
fetch next from cur into @id
end
close cur
deallocate cur