Skip to content

SQL Output Inserted

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:

  1. Auditing: Tracking changes to the database.
  2. Logging: Saving inserted values for later review.
  3. 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 the INSERTED table.
  • The inserted EmployeeID, FirstName, and LastName 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 the EmployeeAudit table.
  • You can add computed columns (like GETDATE() for timestamps) in the OUTPUT 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:

  1. Triggers: When triggers are involved, the OUTPUT clause behaves differently because triggers can modify the inserted data.
  2. Bulk Operations: The OUTPUT clause might not work as expected with certain bulk insert operations, such as BULK INSERT.
  3. 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.

SQL Output Inserted

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!

Getting Help from Steve and the Stedman Solutions Team
We are ready to help. Steve and the team at Stedman Solutions are here to help with your SQL Server needs. Get help today by contacting Stedman Solutions through the free 30 minute consultation form.

Contact Info for Stedman Solutions, LLC. --- PO Box 3175, Ferndale WA 98248, Phone: (360)610-7833
Our Privacy Policy