I’m using SQL Server 2008 and would like to know if there is a way to write a generic update trigger that checks to see if the table being updated has a field called ‘Last_Updated’ and if so updates that field with the getDate() function.
Can I write a single generic trigger like that that that works for all tables in my db?
Thanks for your time!
no, you need to have a trigger for each table, or include
Last_Updated=GETDATEin every UPDATE.try this:
This query will produce a
Create Triggercommand for every table that has aLast_Updatedcolumn. You MUST edit each command and and fix thea.pk=i.pkfor each table to be the proper columns. Then run all the commands and then you’ll have all the triggers. you’ll need to add triggers as you add new tables.