Sybase更新触发 - 检查多行以进行更新
-
14-11-2019 - |
题
我正在在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
.
任何想法如何在我的触发器中绕过这个多更新问题?
解决方案
如果错误在触发器中,则以下应工作: 而不是:
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))
. 其他提示
这应该有效:
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
. 不隶属于 StackOverflow