The OUTPUT clause captures the before-and-after column values of rows touched by data-modification statements and streams them to the client or into another table. Introduced in SQL Server 2005, OUTPUT behaves like a temporary result set that exposes the logical deleted and inserted pseudo-tables. It supports INSERT, UPDATE, DELETE, and MERGE, enabling auditing, change capture, chaining modifications, or avoiding a second query to fetch affected rows. When used with INTO, the results are persisted to a target table or table variable; otherwise they are returned to the caller. The clause applies to exactly the rows modified within the statement, is executed atomically with the DML, and respects transaction boundaries. Limitations: cannot reference text/ntext/image columns directly (use INTO with varchar(max) instead), not allowed with triggers that reference the same table, and cannot be combined with the TOP clause without ORDER BY unless INTO is specified.
INSERTED.column_name
- any column from the rows after modificationDELETED.column_name
- any column from the rows before modificationtarget_table
- table or table variable receiving the output (optional)column_list
- list that maps target_table columns to OUTPUT columns (optional)RETURNING clause, INSERTED table, DELETED table, MERGE, OUTPUT INTO, identity retrieval
SQL Server 2005
You can reference any non-text/ntext/image column from the virtual INSERTED and DELETED tables. Qualify each column with the appropriate alias.
Yes. OUTPUT executes atomically with the data-modification statement. If the transaction rolls back, the output rows are rolled back as well.
Triggers fire after the DML completes and add overhead on every change. OUTPUT captures changes inline, requires no additional object, and can write to a target table without recursion issues.
Yes, but you cannot reference OUTPUT on a table if an AFTER trigger on that same table also uses OUTPUT. Remove one of the clauses or refactor your logic.