tsql - TRIGGER AFTER UPDATE - Get updated data -
i discovering dml triggers in sql server. created trigger registeres inserts 1 table in table. same update statement.
my insert trigger:
create trigger trgsuppliersloginsert on production.suppliers after insert begin insert [production].supplierslog (supplierid, [action], companynamechangedto, contacnamechangedto, contacttitlechangedto, addresschangedto, citychangedto, regionchangedto, postalcodechangedto, countrychangedto, phonechangedto, faxchangedto) select supplierid, 'insert', companyname, contactname, contacttitle, address, city, region, postalcode, country, phone, fax inserted end go
i tried doing same update:
create trigger trgsuppliersloginsert on production.suppliers after update begin insert [production].supplierslog (supplierid, [action], companynamechangedto, contacnamechangedto, contacttitlechangedto, addresschangedto, citychangedto, regionchangedto, postalcodechangedto, countrychangedto, phonechangedto, faxchangedto) select supplierid, 'update', companyname, contactname, contacttitle, address, city, region, postalcode, country, phone, fax updated end go
but know 'updated' table doesn't exist. there other way updated data supplierslog table? thanks.
when update row should use insertet table . can use 1 trigger insert , update :
create trigger trgsuppliersloginsert on production.suppliers after insert,update begin insert [production].supplierslog (supplierid, [action], companynamechangedto, contacnamechangedto, contacttitlechangedto, addresschangedto, citychangedto, regionchangedto, postalcodechangedto, countrychangedto, phonechangedto, faxchangedto) select supplierid, 'insert', companyname, contactname, contacttitle, address, city, region, postalcode, country, phone, fax inserted end go
if deleted (table fill after delete action) has row action update , if deleted not have row action insert
declare @rowcount int , @action varchar set @rowcount = select count(1) deleted if(@rowcount == 0) set @action = 'insert' else @action = 'update'
and use select :
select supplierid, @action, companyname, contactname, contacttitle, address, city, region, postalcode, country, phone, fax inserted
Comments
Post a Comment