Sybase Update Trigger - Vérifiez plusieurs lignes pour la mise à jour
-
14-11-2019 - |
Question
J'écris un déclencheur dans une base de données Sybase ASE qui déclenche une mise à jour et compare la valeur avant et après la mise à jour.Je fais cela pour vérifier si la mise à jour a réellement modifié les données.Si tel est le cas, il ajoute une ligne à une table de surveillance.Malheureusement, la gâchette ne fonctionne que si la commande de mise à jour n'affecte que 1 ligne.Si cela affecte plus d'une ligne, j'obtiens cette erreur:
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.
Mon processus ressemble à ceci:
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
Une idée de la façon dont je peux me contenter de ce problème de mise à jour multiple dans mon déclencheur?
La solution
Si l'erreur est dans la gâchette, les éléments suivants doivent fonctionner: au lieu de:
if update(col_a) and (select col_a from inserted) not in (select col_a from deleted)
utiliser
if update(col_a) and exists (select col_a from inserted where col_a not in (select col_a from deleted))
Autres conseils
Cela devrait fonctionner:
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