Skip to content

ErcanOPAK.com

  • ASP.Net WebForms
  • ASP.Net MVC
  • C#
  • SQL
  • MySQL
  • PHP
  • Devexpress
  • Reportviewer
  • About
SQL

How to use OUTPUT for Insert, Update and Delete in SQL

- 19.09.23 - ErcanOPAK

The OUTPUT clause was introduced in SQL Server 2005. The OUTPUT clause returns the values of each row that was affected by an INSERT, UPDATE, or DELETE statement. It even supports a MERGE statement, which was introduced in SQL Server 2008 version.

The OUTPUT clause has access to two temporary or in-memory SQL tables, INSERTED and DELETED. These tables are populated when an INSERT/UPDATE/DELETE operation is done on a table. As a result, the OUTPUT clause can provide us the affected records by referencing these tables. So let’s see how to do this. But before we proceed with the discussion, we need to discuss key points related to these tables:

  • When a new record is inserted into a table, say Employee, a new record is added to the INSERTED table.
  • When a record is updated in a table, a new record with the old values is added to the DELETED table and a new record, with the new values is added to the INSERTED table.
  • When a record is deleted from a table, a new record, with the values of the record being deleted, is added to the DELETED table.

For our discussion, we will use two tables named Employee and Department. So let’s discuss the cases one by one.

  1. OUTPUT with the INSERT statement: Simply provide the name of the columns you would like to be fetched, using the Inserted.Column_Name syntax, just after the INSERT INTO and before the actual values to be inserted. See the query below:

    OUTPUT with the INSERT statement

  2. OUTPUT with DELETE statement: The concept remains the same, except the Inserted.Column_Name is replaced with DELETED.Column_Name, since we are looking for the records that are affected by the DELETE statement. See the query below:

    OUTPUT with DELETE statement

  3. OUTPUT with UPDATE statement: For any UPDATE operation, the records to be updated are first deleted and then inserted into the temporary tables. So we can get both the old and new record values. Let’s update the records and see the results:

    OUTPUT with UPDATE statement

    Since we can see the results, we have the old value as the Deleted one and the new value as the Inserted one.

  4. OUTPUT with JOIN UPDATE/DELETE: Apart from these operations, we can also use OUTPUT to get the records affected when we are using a JOIN statement to UPDATE/DELETE bulk records. For example, we create a table Department and insert sample data into it. Also,  some related records are inserted into the Employee table. So we have the setup below:

    OUTPUT with JOIN UPDATE or DELETETo test this case, we will update the records to the Employee table, for those who belong to the “HR” department, in other words records with the Ids 1, 2, 18 and 19. So let’s write a query with INNER JOIN, to update the results use the OUTPUT keyword to see the affected records. So we write our query as:

    affected records

Now let’s see how to store these values in temporary tables or table variables. For this, we will add some more records to the table.

Store data into Temporary table: We simply create a new temporary table and insert the records into it using the INTO keyword. See the UPDATE query below, that stores the data into a temporary table:

Store data into Temporary table

Store data into Table variable: No changes except that the temporary table is replaced with a table variable now. We will now use it with the DELETE statement. So our query changes to the following:

Store data into Table variable

This keyword becomes a very powerful option when we have Stored Procedures, where we are manipulating the data and need to know what and how the records are being affected. So this was about the use of the OUTPUT keyword.

Thank you so much Jasminder Singh from C#Corner for this amazing article. This was the best one so far.

Related posts:

How to return only the Date part from SQL Server DateTime datatype
How to generate a random number for each row in T-SQL
How to create Local and Global Temp Tables in MS SQL
How to find the first missing value in a series in MS SQL
Post Views: 69

Post navigation

How to get the first and last date of the current year in SQL
How to convert JSON to XML or XML to JSON in C#

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

October 2024
M T W T F S S
 123456
78910111213
14151617181920
21222324252627
28293031  
« Sep    

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (848)
  • Get the First and Last Word from a String or Sentence in SQL (756)
  • How to select distinct rows in a datatable in C# (703)
  • How to add default value for Entity Framework migrations for DateTime and Bool (584)
  • Add Constraint to SQL Table to ensure email contains @ (521)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (480)
  • How to make theater mode the default for Youtube (465)
  • Average of all values in a column that are not zero in SQL (453)
  • Find numbers with more than two decimal places in SQL (383)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (371)

Recent Posts

  • How to Reset Taskbar in Windows 11
  • Essential Steps to Take After Windows 11 Updates
  • How to list all tables referencing a table by Foreign Key in MS SQL
  • How to format date in Javascript
  • How to generate a random number for each row in T-SQL
  • How to solve ‘Microsoft.TeamFoundation.Git.Contracts.GitCheckoutConflictException’ problem
  • Why nautical mile equals 1852 mt
  • How to Find Day Name From Date in SQL Server
  • How to make pagination in MS SQL Server
  • How to update Identity Column in SQL Server

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (848)
  • Get the First and Last Word from a String or Sentence in SQL (756)
  • How to select distinct rows in a datatable in C# (703)
  • How to add default value for Entity Framework migrations for DateTime and Bool (584)
  • Add Constraint to SQL Table to ensure email contains @ (521)

Recent Posts

  • How to Reset Taskbar in Windows 11
  • Essential Steps to Take After Windows 11 Updates
  • How to list all tables referencing a table by Foreign Key in MS SQL
  • How to format date in Javascript
  • How to generate a random number for each row in T-SQL

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter

© 2024 ErcanOPAK.com

Proudly powered by WordPress | Theme: Xblog Plus by wpthemespace.com