Trigger di aggiornamento Sybase: controlla più righe per l'aggiornamento
-
14-11-2019 - |
Domanda
Sto scrivendo un trigger in un database Sybase ASE che spara su un aggiornamento e confronta il valore prima e dopo l'aggiornamento.Faccio questo per verificare se l'aggiornamento ha effettivamente modificato i dati.Se questo è il caso, aggiunge una riga a una tabella di monitoraggio.Sfortunatamente il trigger funziona solo se il comando di aggiornamento influisce solo a 1 riga.Se sta influenzando più di una riga ottengo questo errore:
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.
.
Il mio Proc sembra questo:
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
.
Qualche idea come posso aggirare questo problema multi aggiornamento nel mio trigger?
Soluzione
Se l'errore è in trigger, il seguente dovrebbe funzionare: Invece di:
if update(col_a) and (select col_a from inserted) not in (select col_a from deleted)
.
Usa
if update(col_a) and exists (select col_a from inserted where col_a not in (select col_a from deleted))
. Altri suggerimenti
Questo dovrebbe funzionare:
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
. Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow