質問

私は更新上で起動するSybase ASEデータベースにトリガを書いていて、更新前後の値を比較しています。更新が実際にデータを変更したかどうかを確認します。これが監視テーブルに行を追加する場合。残念ながら、アップデートコマンドが1行にのみ影響を与える場合にのみ、トリガーは機能します。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
.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top