sql - How to get the action performed in trigger -


what want achieve in question if made update/delete/insert in 1 of table should insert record 1 of table logs purpose

i have 1 table test. suppose if insert below row in table test, should fire trigger , insert row in table logs

test

id  |   name    |   location         1   |   test    |   america  

log

id  |   updatetime              |   action       ----------------------------------------------------------       1   |   2017-04-06 16:51:18.190 |   insert   

and same if delete or update thing under action should have delete or update respectively

id  |   updatetime              |   action       ----------------------------------------------------------       1   |   2017-04-06 16:51:18.190 |   insert   1   |   2017-04-06 16:51:18.190 |   delete   

i have created single trigger

create trigger abc  on test after insert, update, delete begin     declare @id int      select @id = i.id inserted      insert log values (@id, getdate()) end 

from above trigger able id , updatetime, how action column action has been performed how achieve that. can have clue

you can create next approach determining action happens:-

declare @action char(1);     set @action = (case when exists(select * inserted)                          , exists(select * deleted)                         'u'  -- set action updated.                         when exists(select * inserted)                         'i'  -- set action insert.                         when exists(select * deleted)                         'd'  -- set action deleted.                     end)  -- getting id if  @action = 'd' select @id=i.id deleted else -- insert or update select @id=i.id inserted 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -