2012-02-29 51 views
0

我正在Sybase ASE數據庫中寫入觸發更新的觸發器,並比較更新前後的值。我這樣做來驗證更新是否實際更改了數據。如果是這樣的話,它會在監控表中添加一行。不幸的是,觸發器只有在更新命令隻影響1行時纔有效。如果影響多行我得到這個錯誤:sybase更新觸發器 - 檢查多個行以進行更新

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

把所有的SP,請。 – 2012-02-29 16:29:31

+0

@aF。我編輯了上面的代碼 – dom 2012-02-29 16:38:18

回答

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)) 
+0

這不工作 - 仍然得到相同的錯誤: – dom 2012-02-29 16:27:38

+0

編輯我的答案。請嘗試。 – Vikram 2012-02-29 16:39:27

+0

這是我的解決方案,試試@dom – 2012-02-29 16:52:10

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