Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Update date to a current date in the event of value chan
Message
From
16/06/2010 10:08:04
 
 
To
16/06/2010 10:00:01
General information
Forum:
Microsoft SQL Server
Category:
Stored procedures, Triggers, UDFs
Miscellaneous
Thread ID:
01469101
Message ID:
01469222
Views:
45
Here's the code for an update trigger, using your table and column names. (You'll need to change the reference of "primaryKey" to the name of your primary key column in your inventory table)

Note that the trigger exposes two system tables from the transaction log...DELETED (which contains the row or rows updated to begin with, BEFORE the update), and INSERTED (which contains the row or rows updated, AFTER the update). You join them together, and in the WHERE clause, only use those where the standard cost column has changed. Let me know if this helps...

So yes, you CAN create a trigger on a table, and inside the trigger, update the very table that caused the trigger to fire (and by default, it WON'T "re-fire" the trigger recursively)
CREATE TRIGGER Inventory_Update_Trigger
ON Inventory
AFTER UPDATE

AS

BEGIN

   UPDATE Inventory 
                 SET dStndCost = GETDATE()
                     FROM Inventory
                           JOIN Inserted on Inventory.PrimaryKey = Inserted.PrimaryKey
                           JOIN Deleted on Inventory.PrimaryKey = Deleted.PrimaryKey
                                  WHERE Inserted.nStndCost <> Deleted.nStndCost
END
GO
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform