SQL Output Inserted: Capturing Inserted Values with the OUTPUT Clause
When working with SQL Server, there are many scenarios where you need to capture and track the values inserted into a table. Whether you’re logging changes, auditing data, or simply wanting to use those values elsewhere in your application, the OUTPUT clause provides a powerful way to achieve this.
In this article, we’ll discuss how to use the OUTPUT
clause to capture values inserted into a table, with a focus on the sql output inserted
use case.
What is the OUTPUT Clause?
The OUTPUT
clause in SQL Server is a versatile feature that allows you to return the results of INSERT
, UPDATE
, DELETE
, or MERGE
operations. Specifically, for an INSERT
statement, the clause can be used to output the values that were added to the target table.
This feature is particularly useful for:
- Auditing: Tracking changes to the database.
- Logging: Saving inserted values for later review.
- Chained Processing: Using the inserted values in subsequent operations without re-querying the database.
Using OUTPUT with INSERT: The Basics
When inserting rows into a table, the OUTPUT
clause lets you return the values of the inserted rows by referencing the special INSERTED
table. This table is a pseudo table that holds the new values being added to the database.
Here’s a basic example:
CREATE TABLE Employees ( EmployeeID INT IDENTITY(1,1) PRIMARY KEY, FirstName NVARCHAR(50), LastName NVARCHAR(50), HireDate DATE ); -- Insert new employees and output the inserted values INSERT INTO Employees (FirstName, LastName, HireDate) OUTPUT INSERTED.EmployeeID, INSERTED.FirstName, INSERTED.LastName VALUES ('John', 'Doe', '2024-01-01'), ('Jane', 'Smith', '2024-02-01');
In this example:
- The
OUTPUT
clause retrieves values from theINSERTED
table. - The inserted
EmployeeID
,FirstName
, andLastName
are displayed as the result of the query.
Storing Inserted Values in Another Table
In some cases, you might want to save the inserted values into an audit or logging table for later use. You can easily redirect the output of the OUTPUT
clause into another table. Here’s how:
CREATE TABLE EmployeeAudit ( AuditID INT IDENTITY(1,1), EmployeeID INT, FirstName NVARCHAR(50), LastName NVARCHAR(50), InsertedDate DATETIME DEFAULT GETDATE() ); -- Insert into Employees and log the inserted rows in EmployeeAudit INSERT INTO Employees (FirstName, LastName, HireDate) OUTPUT INSERTED.EmployeeID, INSERTED.FirstName, INSERTED.LastName, GETDATE() INTO EmployeeAudit (EmployeeID, FirstName, LastName, InsertedDate) VALUES ('Alice', 'Johnson', '2024-03-01'), ('Bob', 'Brown', '2024-04-01');
In this example:
- The
OUTPUT INTO
clause redirects the inserted values into theEmployeeAudit
table. - You can add computed columns (like
GETDATE()
for timestamps) in theOUTPUT
clause.
This approach is perfect for audit logging or tracking inserted records.
Using OUTPUT for Identity Values
When inserting rows into a table with an IDENTITY
column, the OUTPUT
clause is an excellent way to retrieve the auto-generated values without requiring a separate SCOPE_IDENTITY()
call or SELECT
statement.
-- Retrieve the newly generated EmployeeID values INSERT INTO Employees (FirstName, LastName, HireDate) OUTPUT INSERTED.EmployeeID VALUES ('Charlie', 'Green', '2024-05-01');
Here, the OUTPUT
clause directly returns the newly generated EmployeeID
values, which can be used in the application or stored elsewhere.
Combining OUTPUT with Transactions
If you’re performing an INSERT
as part of a transaction, you can still use the OUTPUT
clause to capture inserted values. For example:
BEGIN TRANSACTION; -- Insert data and capture inserted values INSERT INTO Employees (FirstName, LastName, HireDate) OUTPUT INSERTED.EmployeeID, INSERTED.FirstName, INSERTED.LastName VALUES ('Daniel', 'Gray', '2024-06-01'); COMMIT TRANSACTION;
The OUTPUT
clause works seamlessly with transactions, allowing you to capture and process inserted data even in complex workflows.
Limitations of the OUTPUT Clause
While the OUTPUT
clause is incredibly powerful, it’s worth noting a few limitations:
- Triggers: When triggers are involved, the
OUTPUT
clause behaves differently because triggers can modify the inserted data. - Bulk Operations: The
OUTPUT
clause might not work as expected with certain bulk insert operations, such asBULK INSERT
. - Computed Columns: The
OUTPUT
clause cannot directly reference computed columns.
Learn More: Take Our SQL Shorts Course!
If you’re interested in mastering the OUTPUT
clause and learning how to use it effectively in real-world scenarios, check out our SQL shorts course, Using the TSQL Output Clause. This quick and practical course will teach you how to:
- Leverage the
OUTPUT
clause for auditing and logging. - Redirect output to other tables.
- Optimize queries with identity column retrieval.
Click here to enroll now: Using the TSQL Output Clause.
The sql output inserted
functionality provided by the OUTPUT
clause is a game-changer for SQL Server developers and DBAs. Whether you’re capturing inserted values for auditing, logging, or further processing, the OUTPUT
clause simplifies workflows and reduces the need for redundant queries.
For more tips and in-depth training, don’t forget to explore our course on using the OUTPUT
clause. Start using this feature effectively today and see how it transforms your SQL Server workflows!