Sybase Update Trigger - Verifique múltiples filas para actualizar
-
14-11-2019 - |
Pregunta
Estoy escribiendo un disparador en una base de datos Sybase ASE, que dispara en una actualización y compara el valor antes y después de la actualización.Hago esto para verificar si la actualización ha cambiado realmente los datos.Si este es el caso, agrega una fila a una tabla de monitoreo.Desafortunadamente, el disparador solo funciona si el comando de actualización solo afecta a 1 fila.Si está afectando más de una fila, obtengo este error:
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.
Mi proc se ve así:
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
¿Alguna idea de cómo puedo conseguir en este problema de actualización múltiple dentro de mi gatillo?
Solución
Si el error está en desencadenante, entonces el siguiente debe funcionar: en lugar de:
if update(col_a) and (select col_a from inserted) not in (select col_a from deleted)
Uso
if update(col_a) and exists (select col_a from inserted where col_a not in (select col_a from deleted))
Otros consejos
Esto debería funcionar:
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
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow