I have the trigger in one table and would like to read UserId
value when a row is inserted, updated or deleted. How to do that? The code below does not work, I get error on UPDATED
ALTER TRIGGER [dbo].[UpdateUserCreditsLeft] ON [dbo].[Order] AFTER INSERT,UPDATE,DELETE AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; DECLARE @UserId INT, SELECT @UserId = INSERTED.UserId FROM INSERTED, DELETED UPDATE dbo.[User] SET CreditsLeft = CreditsLeft - 1 WHERE Id = @UserId END
Triggers have special INSERTED and DELETED tables to track "before" and "after" data. So you can use something like IF EXISTS (SELECT * FROM DELETED) to detect an update. You only have rows in DELETED on update, but there are always rows in INSERTED . Look for "inserted" in CREATE TRIGGER.
INSERT , UPDATE , and DELETE are all functions in SQL that help you ensure your data is up-to-date and kept clear of unnecessary or outdated information. INSERT , UPDATE , and DELETE , as well as SELECT and MERGE, are known as Data Manipulation Language (DML) statements, which let SQL users view and manage data.
The CREATE TRIGGER statement allows you to create a new trigger that is fired automatically whenever an event such as INSERT , DELETE , or UPDATE occurs against a table. In this syntax: The schema_name is the name of the schema to which the new trigger belongs. The schema name is optional.
Update trigger to capture Updates and Operation You can use the following script to update the table to add the new column and the trigger to capture updates, along with inserts and deletes.
Please note that inserted, deleted
means the same thing as inserted CROSS JOIN deleted
and gives every combination of every row. I doubt this is what you want.
Something like this may help get you started...
SELECT CASE WHEN inserted.primaryKey IS NULL THEN 'This is a delete' WHEN deleted.primaryKey IS NULL THEN 'This is an insert' ELSE 'This is an update' END as Action, * FROM inserted FULL OUTER JOIN deleted ON inserted.primaryKey = deleted.primaryKey
Depending on what you want to do, you then reference the table you are interested in with inserted.userID
or deleted.userID
, etc.
Finally, be aware that inserted
and deleted
are tables and can (and do) contain more than one record.
If you insert 10 records at once, the inserted
table will contain ALL 10 records. The same applies to deletes and the deleted
table. And both tables in the case of an update.
EDIT Examplee Trigger after OPs edit.
ALTER TRIGGER [dbo].[UpdateUserCreditsLeft] ON [dbo].[Order] AFTER INSERT,UPDATE,DELETE AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; UPDATE User SET CreditsLeft = CASE WHEN inserted.UserID IS NULL THEN <new value for a DELETE> WHEN deleted.UserID IS NULL THEN <new value for an INSERT> ELSE <new value for an UPDATE> END FROM User INNER JOIN ( inserted FULL OUTER JOIN deleted ON inserted.UserID = deleted.UserID -- This assumes UserID is the PK on UpdateUserCreditsLeft ) ON User.UserID = COALESCE(inserted.UserID, deleted.UserID) END
If the PrimaryKey of UpdateUserCreditsLeft
is something other than UserID, use that in the FULL OUTER JOIN instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With