我想在一个insert语句中插入多个记录,那时我的触发器中出现错误
错误是
Msg 512, Level 16, State 1, Procedure SetIntFlag, Line 7 Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression. The statement has been terminated.
我的触发器是
declare @act char(16)if update (first_nm)
set @act = (select acct_no from inserted)
update Client set Phn9_int = 1 where acct_no = @act
如果多个列插入其显示错误,则将其分配给一个变量。
答案 0 :(得分:2)
假设这是整个触发器:
update Client set Phn9_int = 1 where acct_no in (
select acct_no from inserted)
基本上,我的经验法则是如果你在触发器中看到任何标量变量,它可能会在多行操作方面被打破。例外情况是它使用游标,但游标通常可以避免使用。
答案 1 :(得分:0)
将光标声明插入并使用它,代码看起来应该类似于bellow:
DECLARE @act char(16)
DECLARE my_Cursor CURSOR FAST_FORWARD FOR SELECT acct_no FROM INSERTED;
OPEN my_Cursor
FETCH NEXT FROM my_Cursor into @act
WHILE @@FETCH_STATUS = 0
BEGIN
UPDATE Client SET Phn9_int = 1 where acct_no = @act
FETCH NEXT FROM my_Cursor into @act
END
CLOSE my_Cursor
DEALLOCATE my_Cursor
答案 2 :(得分:0)
不要在WHERE子句中使用local_variable作为条件。而是使用简单的JOIN条件
if update (first_nm)
update c
set c.Phn9_int = 1
FROM Client c JOIN inserted i ON c.acct_no = i.acct_no