2012-02-29 4 views
0

Sybase ASEデータベースにトリガを書き込んで、更新プログラムを起動し、更新前後の値を比較します。私は、更新が実際にデータを変更したかどうかを確認するためにこれを行います。この場合、監視テーブルに行が追加されます。残念ながら、トリガは、updateコマンドが1行だけに影響を与える場合にのみ機能します。それは複数の行に影響している場合、私はこのエラーを取得:sybase update trigger - 複数行の更新を確認します

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をしてください置きます。 –

+0

@aF。私は上記のコードを編集しました – dom

答えて

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

+0

私の答えを編集しました。それを試してみてください。 – Vikram

+0

それは私の解決策でした。試してみてください。@dom –

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