Get inserted or deleted records in SQL - OUTPUT Clause

In SQL, how we identify the last inserted or deleted records? We’ve used @@identity to get the last inserted records identity value. This will not be sufficient while insert lot of records in same time. To solve that, we have OUTPUT clause to get the inserted/deleted data information.

Especially this OUTPUT clause is very useful while using MERGE clause. Since this is the only way to get inserted/deleted records in TARGET table.

OUTPUT Clause:
Returns information from, or expressions based on, each row affected by an INSERT, UPDATE, DELETE, or MERGE statement. These results can be returned to the processing application for use in such things as confirmation messages, archiving, and other such application requirements. The results can also be inserted into a table or table variable. Additionally, you can capture the results of an OUTPUT clause in a nested INSERT, UPDATE, DELETE, or MERGE statement, and insert those results into a target table or view.


Syntax:
::=
{
    [ OUTPUT INTO { @table_variable | output_table } [ ( column_list ) ] ]
    [ OUTPUT ]
}
::=
{ | scalar_expression } [ [AS] column_alias_identifier ]
    [ ,...n ]

::=
{ DELETED | INSERTED | from_table_name } . { * | column_name }

    | $action

For more understanding go to below link.

Comments

Popular posts from this blog

BDD - Acceptance Test Driven Development

Angular JS – Part 2

.Net Collections