How to store record before it updated? - sql-server

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

Related

Delete one cell from one row in a SQL Server table with WHERE condition

I have a SQL Server database table Person and it has a column PersonPhoto (data is binary picture) and I need to delete data from this column where PersonCode = '386086'. How can I do this with T-SQL?
You can't delete single column from the table, either you should delete one row or update particular column.
Try simple Update
UPDATE Person
SET PersonPhoto = NULL
WHERE PesronCode='386086'.
Or go with row Delete
DELETE FROM Person WHERE PesronCode='386086'.
You cannot delete data from just a field in SQL Server, but you can update the field to null (if it's nullable).
Update Person Set PersonPhoto = NULL where PesronCode='386086'

SQL Trigger inserted deleted table contents on UPDATE

I know from (Insert Update trigger how to determine if insert or update) that an UPDATE transaction causes rows in the deleted table
what are the contents of inserted and deleted when updating a single row?
Inserted contains that one row being updated with the new values (after the UPDATE), while Deleted contains that one row being updated with the old values (before the UPDATE)

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).

What should be the table contain updated row for update trigger

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

Resources