我在Sybase ASE数据库中编写一个触发器,它触发更新并比较更新前后的值。我这样做是为了验证更新是否实际更改了数据。如果是这种情况,它会向监控表添加一行。不幸的是,只有当update命令只影响1行时,触发器才有效。如果它影响多行,我会收到此错误:
Msg 512, Level 16, State 5:
Server 'myserver', Procedure 'myproc', Line 2:
Subquery returned more than 1 value. This is illegal when the subquery follows =, !=, <, <= , >, >=, or when the subquery is used as an expression.
我的proc看起来像这样:
create trigger mytrigger on mytable for update as
set nocount on
/* now do the insert if colum was updated */
if update(col_a) and (select col_a from inserted) not in (select col_a from deleted)
begin
insert into monitoring_table (login,tablename,field,action,pstamp)
select suser_name(),'mytable','col_a','U',getdate() from inserted
end
/* now do the insert if colum was updated */
if update(col_b) and (select col_b from inserted) not in (select col_b from deleted)
begin
insert into monitoring_table (login,tablename,field,action,pstamp)
select suser_name(),'mytable','col_b','U',getdate() from inserted
end
go
任何想法如何在我的触发器中解决这个多更新问题?
答案 0 :(得分:2)
如果错误在触发器中,则以下内容应该有效: 而不是:
if update(col_a) and (select col_a from inserted) not in (select col_a from deleted)
使用
if update(col_a) and exists (select col_a from inserted where col_a not in (select col_a from deleted))
答案 1 :(得分:1)
这应该有效:
create trigger mytrigger on mytable for update as
set nocount on
/* now do the insert if colum was updated */
if update(col_a) and (select col_a from inserted where col_a not in (select col_a from deleted)
begin
insert into monitoring_table (login,tablename,field,action,pstamp)
select suser_name(),'mytable','col_a','U',getdate() from inserted
end
/* now do the insert if colum was updated */
if update(col_b) and (select col_b from inserted where col_b not in (select col_b from deleted)
begin
insert into monitoring_table (login,tablename,field,action,pstamp)
select suser_name(),'mytable','col_b','U',getdate() from inserted
end
go