Я смотрю немного направления или указания, почему я получаю несколько строк, используя мой триггер. В основном у меня есть веб-приложение, которое контролирует типы активов (например, ноутбуки, телефоны и т. Д. c), и я пытаюсь сделать с этим триггером, когда имя типа актива (at_typedes c) изменяется, и я записываюсь в аудит table (в данном случае sql_log), какое было старое имя и какое новое.
Это работает, но по какой-то причине я получаю несколько строк, написанных в операторе INSERT TO SQL_LOG. Он записывает старое имя и новое имя, но затем я получу еще 3 строки, в которых старое имя показывает новое имя ...
Это в настоящее время на сервере 2008 SQL.
-- create the trigger
go
create trigger trg_InsteadOfUpdate on [dbo].[lkp_asset_types]
instead of update
as
begin
DECLARE @triggerAction varchar(1)
-- determine the TRIGGER action
-- this allows us to tell if its an INSERT or an UPDATE
SELECT @triggerAction = CASE
WHEN EXISTS(SELECT 1 FROM INSERTED)
AND EXISTS(SELECT 1 FROM deleted) THEN 'U'
WHEN EXISTS(SELECT 1 FROM inserted) THEN 'I'
ELSE 'D' END;
-- get the orginally asset name from the DELETED table
-- this contains the rows as they were BEFORE the UPDATE Statement
DECLARE @orgAssetTypeName varchar(255)
SET @orgAssetTypeName = (SELECT top 1 at_typedesc from lkp_asset_types WHERE at_id = (select at_id from deleted))
-- UPDATE to the new asset name based on the NEW value in the INSERTED Table
update lkp_asset_types
set at_typedesc = (select at_typedesc from inserted)
where at_id = (select at_id from inserted)
-- get the new asset name from the INSERTED table
-- this contains the rows as they were AFTER the UPDATE Statement
DECLARE @newAssetTypeName varchar(255)
SET @newAssetTypeName = (SELECT top 1 at_typedesc from lkp_asset_types WHERE at_id = (select at_id from inserted))
insert into sql_log
(sql_log)
values ('SQL PRE Changed from : ' + @orgAssetTypeName + ' to: ' + @newAssetTypeName + '. Action = ' + @triggerAction)
end
go