What should be the table contain updated row for update trigger - sql-server

A while ago i read the article for Trigger in SQL Server, and it said that i can use the Logical Table "Updated" for updated rows... And i got error:
System.Data.SqlClient.SqlException: Invalid object name 'Updated'.
After a while of google, i found out some more post that said only 2 logical tables available are: Inserted and Deleted...
I'm confused... What should i use since my Trigger rely on the Updated table that contain the updated row, and use it to insert to another table or the same table with new PK...
Thank you very much

The two dummy tables are called Inserted (available in INSERT and UPDATE triggers) and Deleted (available in DELETE and UPDATE triggers).
There is no Updated dummy table in SQL Server triggers.
For an FOR UPDATE trigger, the Deleted table contains the old values, while the Inserted table contains the new ones.
Marc

Related

Use one SQL Server trigger that listens on updates of multiple tables

I have a Microsoft SQL Server 2012 database with multiple tables.
All tables contain the same two columns DataRowModified (type datetime) and DataRowLastAuthor (type nvarchar(MAX)). And no, I can't put all those columns into a separate table, it's a requirement that each table directly contains those rows.
I wrote the trigger below for the table Events to automatically update the values of those two columns whenever a row gets updated:
CREATE TRIGGER [dbo].[Trigger_Events_UpdateMetadata]
ON [dbo].[Events]
FOR UPDATE
AS
BEGIN
UPDATE [dbo].[Events]
SET [DataRowModified] = GETDATE(),
[DataRowLastAuthor] = ORIGINAL_LOGIN()
WHERE [Id] IN (SELECT [Id] FROM INSERTED)
END
Now my question is whether I have to copy (and rename) this trigger for every table I have to use it with, or can I somehow write a global trigger that works on all (or a specified set of) tables? It has to know in which table/row the update happened though, because it has to modify it.
What would be the easiest way to implement an automatically maintained LastAuthor and LastModificationDate column into many tables as described?
A trigger in SQL Server is always bound to a single table - you cannot have "global" triggers or triggers attached to multiple tables at once.
If you need a trigger on your 50 tables - you need to write 50 trigger, one each for every table. No way around this.
The only way to avoid this would be to update those columns in your database layer of your application, so that those values would already be present when you save your row of data. Things like Entity Framework allow such "bulk operations" on multiple entities to e.g. update a last modified date and last user to modify the entity.
No, But multiple triggers could invoke the same stored procedure.

How to store record before it updated?

I use a SQL Server database. I have two tables Inspections and History_Inspections.
Before a record from the Inspections table is updated, I need to store it into the History_Inspections table.
I wanted to use a SQL Server update trigger but it fired only after the record is updated.
Any idea how can I store record from Inspections table in History_Inspections table before it updated ?
When an AFTER UPDATE trigger in SQL Server fires, the Inserted pseudo table inside the trigger contains the new values (after the UPDATE), and the Deleted pseudo table contains the old values (before the UPDATE).
So in your case, just read out the rows from the Deleted table and store those into your History_Inspections table.
CREATE TRIGGER trg_AfterUpdateInspections
ON dbo.Inspections
AFTER UPDATE
AS
INSERT INTO dbo.History_Inspections(list of columns)
SELECT (list of columns)
FROM Deleted

SQL Server - How to update only the inserted row of table through trigger?

The problem is I have inserted a new row in a table through procedure which have a AFTER INSERT trigger on it, what I want to do is to update only this new inserted row though trigger, is there any way to do this?
Yes.
Have you tried reading the documentation? I mean, at least once.
THe part where it tells you the trigger has a virtual table (inserted) with all the rows it processes (may be more than one actually, an insert can cover more than one row) and you can then use standard SQL on this (i.e. to find the updated rows in the real table and then update them)?
Your question is the textbook example of a simple standard tutorial trigger chapter 1.
A little google fu - "tsql trigger update inserted table".
Item 1: http://technet.microsoft.com/en-us/library/ms191300.aspx, named "Use the inserted and deleted Tables"
You could also just use search here instead of asking.
SQL Insert trigger to update INSERTED table values

Finding out the data a row has been inserted into a table

Is there a way to find out the data a row has been inserted (into a SQL Server 2005, 2008 or 2008 r2) database table? Without setting up auditing (either ootb or custom 3rd party product).
Thanks
You can always create a trigger on that table. Like so:
create trigger InsertNotification
on YourTable
after insert
as
-- do whatever you want when an insert happens
go
This can definitely be seen as a form of "auditing", but I'm not familiar with "ootb", nor is this a 3rd party product. Triggers are the way to go.
Well if you want to be notified when the row is inserted make insert trigger to this table.
If you just want to save the information when a specific row was inserted, you could just create a new datetime or smalldatetime column with getdate() as the default value.
Whenever a new row is inserted, this column will be automatically filled with the current date/time.
Advantages:
no trigger or 3rd party tool needed
Disadvantages:
this only works for new tables (or all new records in existing tables). If a table already has existing records, you won't have an insert date/time for them
if you want this for all your tables, you have to insert the column into each table

SQL Server Trigger - Insert the last updated date into table linked with foreign key

I am using SQL Server 2008 R2.
I have two tables, ShoppingList and ShoppingListFood. ShoppingListFood is linked to ShoppingList via a foreign key on ShopListID. I wish to update the LastModifiedDate column in the ShoppingList table whenever there is any update / insert done at the ShoppingListFood table. Is it possible??
I googled and found that I need create trigger in stored procedure, but i know nothing about it, can anyone help??
Sure - no problem - try something like this:
CREATE TRIGGER trg_UpdateShoppingListFood
ON dbo.ShoppingListFood
AFTER INSERT, UPDATE
AS
UPDATE dbo.ShoppingList
SET LastModifiedDate = GETDATE()
WHERE ShopListID IN (SELECT DISTINCT ShopListID FROM Inserted)
Basically, this creates a trigger that will fire after INSERT and UPDATE on ShoppingListFood - it will update the LastModifiedDate to "right now" for all rows that have been inserted and/or updated, based on the pseudo table Inserted that's available inside a trigger.
This pseudo table contains all rows that have been freshly inserted (with all their columns), as well as the new values for all rows that have been updated (again: all columns).

Resources