How to find when a row is inserted or updated in SQL Server table - sql

I have a table in SQL Server but there is no column to show when the value is inserted into the table. And also I don't want to create a one.
Is there a way that I can find the time and date when some one has inserted a row into a SQL Server 2008 table or when it is updated?

If you don't want to modify your existing tables, the only way to go is to create another table to log the activities to. Then you can use ON INSERT / ON UPDATE triggers on the source tables and log the time and date of inserts and updates to the log table.

You can use On Insert and On Update Triggers in sql server 2005/2008 and your required fields are available in inserted and updated tables, but if you need to use this information later need to add a new table and log your information on to that!
I myself for doing such stuff like this always add 2 column to my tables
LastActivityBy relation to user table
and
LastActivityOn datetime
so you don't need to use any other table.

If you dont want to alter your existing schema, create a audit table and create trigger on source table to update audit table.

Related

Difference between magic table and temporal (system versioned) table in SQL Server?

What is the difference between magic table and temporal (system versioned) table in SQL Server?
Magic Table
Virtual and No Physical Existence. SQL Server internally maintain magic table.
There are two table name INSERTED and DELETED.
INSERTED contains information about newly inserted or updated record in table.
DELETED contains information about last state of that table record.
Now if you perform two update operation on two table then INSERTED and DELETED megic table updated accoridignly and atleast available in trigger.
Apart from Trigger you can use with output clause.
Temporal Table
This is new feature of SQL Server 2016.
It is also called server versioned history table for particular table so there is a physical existence of this Temporal table.
You can query against it and also its purpose is to keep history of particular record.

Create a trigger for making a single audit table in sql server

How do I create a trigger in Microsoft SQL server to keep a track of all deleted data of any table existing in the database into a single audit table? I do not want to write trigger for each and every table in the database. There will only be once single audit table which keeps a track of all the deleted data of any table.
For example:
If a data is deleted from a person table, get all the data of that person table and store it in an XML format in an audit table
Please check my solution that I tried to describe at SQL Server Log Tool for Capturing Data Changes
The solution is build on creating trigger on selected tables dynamically to capture data changes (after insert, update, delete) and store those in a general table.
Then a job executes periodically and parses data captured and stored in this general table. After data is parsed it will be easier for humans to understand and see easily which table field is changed and its old and new values
I hope this proposed solution helps you for your own solution,

create table using exsiting table update/link to column

I want to create a new table with one of the columns linked/updated by a table on another database (but on the same server).
so when table A column is updated it will automatically update table b's column with the same information no data will be entered into this column from table b.
I have tried various different ways but can't find a way to do this with out updating column manually or setting up a server agent any help would be great.
if you want to make cross server query, please check sp_addlinkedserver
https://msdn.microsoft.com/en-us/library/ms190479.aspx
Once it is linked, just create a trigger, where you can use
select * from [server].[database].[schema].[table]

SQL - Record Every Statement That Updated Table

I am working on a database used by separate applications. One of these applications is updating two fields in a table but I can't work out what one and don't have the source code for all the applications.
I am wondering if it is possible to write a log (to another table or elsewhere) to what the last update statement made against the table in question was. E.g. to record all SQL that has attempted to update the table automatically...
create a trigger before update on this table. Also create a new table. In that trigger store values before and after update in to a newly created table

one sql trigger insert,update,delete for all tables in the database

is it possible to declare one Sql Trigger Insert, Update, Delete for all tables in the database instead of creating a separate trigger for each table? I just want a simple history of what actions have been taken e.g. TABLE A deletes a row ,TABLE B updates a row ,TABLE C Add new row .. and the trigger will insert a new row to another table with that information I want.
No, a trigger can only be defined on a specified table.
You should read up on the auditing features of SQL Server (http://technet.microsoft.com/en-us/library/cc280386.aspx). They are more performant and more flexible in what you want to achieve. Unfortunately they are not available in the Express Edition.