Hi,
I'm having a problem with this trigger:
ALTER TRIGGER [dbo].[trg_B_U_Login]
ON [dbo].[Login]
FOR UPDATE
AS
BEGIN
IF @@ROWCOUNT = 0
RETURN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
DECLARE @Campos nvarchar(500);
DECLARE @Old_DataRemocao nvarchar(20);
DECLARE @New_DataRemocao nvarchar(20);
DECLARE @Old_IDCriador nvarchar(10);
DECLARE @New_IDCriador nvarchar(10);
SELECT @Campos='';
IF(Exists(SELECT * FROM deleted WHERE DataRemocao is NULL))
BEGIN
SELECT @Old_DataRemocao='NULL';
END
ELSE
BEGIN
SELECT @Old_DataRemocao=(SELECT * DataRemocao FROM deleted);
END
IF(Exists(SELECT * FROM inserted WHERE DataRemocao is NULL))
BEGIN
SELECT @New_DataRemocao=NULL;
END
ELSE
BEGIN
SELECT @New_DataRemocao=(SELECT * DataRemocao FROM inserted);
END
IF(@Old_DataRemocao<>@New_DataRemocao)
BEGIN
SELECT @Campos=@Campos+'DataRemocao={'+@Old_DataRemocao+' -> ' + @New_DataRemocao +'}; ';
END
IF(EXISTS(SELECT * FROM deleted as del, inserted AS ins WHERE ins.Login<>del.Login))
BEGIN
SELECT @Campos=@Campos+'Login={'+ del.Login +' -> '+ ins.Login+'}; ' FROM deleted as del, inserted as ins
END
......
IF(EXISTS(SELECT * FROM deleted as del, inserted AS ins WHERE ins.DataCriacao<>del.DataCriacao))
BEGIN
SELECT @Campos=@Campos+'DataCriacao={'+ del.DataCriacao +' -> '+ ins.DataCriacao+'}; ' FROM deleted as del, inserted as ins
END
IF(EXISTS(SELECT * FROM deleted as del, inserted AS ins WHERE ins.Nome<>del.Nome))
BEGIN
SELECT @Campos=@Campos+'Nome={'+ del.Nome +' -> '+ ins.Nome+'}; ' FROM deleted as del, inserted as ins
END
IF(@Campos<>'')
BEGIN
INSERT Login_Hs (IDUtilizador, Tabela, Metodo, Chave, Campos, Data)
SELECT '1', 'Login', 'UPDATE', 'IDLogin='+Convert(nvarchar,ID),
@Campos, CONVERT(DATETIME, GETDATE(), 105)
FROM inserted
END
END
that saves the changes made in each row that is updated and is being executed by this update
UPDATE Login
SET DataRemocao = CONVERT(datetime,GETDATE(),105)
WHERE IDPerfil = 25 AND DataRemocao IS NULL;
which is updating more than one row
and sql server 2008 is giving this error:
Msg 512, Level 16, State 1, Procedure trg_B_U_Login, Line 83
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
The statement has been terminated.
This just works when I update a single line. What can i do to resolve this?