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

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.

Related

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,

Data Agent - SELECT from one table and insert into another

Is there any type of product where I can write a SQL statement to select from one table and then insert into another database (The other database is out in the cloud). Also, it needs to be able to check to see if that record exists and then update the row if anything has changed. Then it will need to run every 10-30 minutes to check to see what has changed or if new records have been added.
The source database and the ending database have a different schema (if that matters?) I've been looking, but it seams that only products out there are ones that will just copy one table and insert into a table with the same schema.

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

How to update a table copy in another database

I have two identical databases - one for development (DEV) and one for production (PROD) (both SQL Server 2008).
I have updated the contents of a certain table in DEV and now I want to sync the corresponding table in PROD.
I have not changed the table schema, only some the data inside the table (I have both changed existing rows and added some new rows).
How can I easily transfer the changes in DEV to the corresponding table in PROD?
Note, that the values in the automatic identity column mgiht not match exactly between the two tables. However, I know that I have only made changes to rows having the same value in another column.
Martin
If you don't want to use the replication, you can Create update, Insert and delete trigger in DEV database and update PROD by trigger.
or you can create view of DEV database table on the PROD database.

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

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.