SQL Server Error 179, *"Cannot use the OUTPUT option when passing"*, occurs when your T-SQL script tries to use the OUTPUT clause with a parameterized query but the syntax or configuration doesn’t allow it. This error often happens during INSERT, UPDATE, or MERGE operations with dynamic SQL or stored procedures that mix string literals and parameters. The fix involves ensuring the OUTPUT clause aligns with parameterized execution plans, often requiring changes to how parameters are passed or how the script is structured.

What You'll Need

  • Access to a SQL Server instance (local or remote).
  • Basic T-SQL knowledge (INSERT, UPDATE, OUTPUT clauses).
  • A query or stored procedure generating Error 179.
  • Permissions to modify scripts or test alternatives.

Step-by-Step

  1. Identify the query or stored procedure causing the error.

    Run the script that triggers Error 179 and examine its structure. Look for:

    • Use of the OUTPUT clause (e.g., `OUTPUT inserted.*` or `OUTPUT deleted.*`).
    • Dynamic SQL with string concatenation (e.g., `EXEC('INSERT INTO table VALUES (@val)')`).
    • Parameterized queries where `@param` is used directly in place of a column reference.

    Example of a problematic script: EXEC('INSERT INTO Orders (CustomerID, Amount) VALUES (123, @amount) OUTPUT inserted.*'), @amount = 1000 This fails because the OUTPUT clause isn’t supported when the query is executed with dynamic text and parameters.

  2. Rewrite the query statically instead of dynamically.

    Dynamic SQL (e.g., `EXEC('SELECT ...')`) often bypasses SQL Server’s parameter handling for OUTPUT. Replace it with a static query: INSERT INTO Orders (CustomerID, Amount) VALUES (123, @amount) OUTPUT inserted.*

    If you must use dynamic SQL, avoid OUTPUT: DECLARE @sql NVARCHAR(MAX) = 'INSERT INTO Orders (CustomerID, Amount) VALUES (123, @amount)'; EXEC(@sql, @amount = 1000)

    For stored procedures, define parameters explicitly (e.g., `@param INT`) instead of mixing literals and parameters.

  3. Ensure parameters match the OUTPUT clause’s context.

    If you’re using a transaction with OUTPUT, ensure:

    • The OUTPUT clause targets a table modified by the same transaction.
    • No intermediate steps (e.g., temporary tables) alter the data before OUTPUT.
    • All outputs are from INSERT/UPDATE operations, not SELECTs.

    Example of a correct structure: BEGIN TRANSACTION; INSERT INTO Products (ID, Name) VALUES (456, 'New Product') OUTPUT inserted.* INTO #TempOutput; SELECT * FROM #TempOutput;

  4. Check for version-specific limitations.

    Error 179 may arise in older SQL Server versions (e.g., 2008) due to OUTPUT clause restrictions. Upgrade or:

    • Use sp_executesql instead of dynamic SQL.
    • Replace OUTPUT with temporary tables or table variables.

    For SQL Server 2012+, try: EXEC sp_executesql N'INSERT INTO Orders (CustomerID, Amount) VALUES (@val)', N'@val INT', @val = 1000

    But note that `sp_executesql` still doesn’t support OUTPUT directly. Instead, use: DECLARE @val INT = 1000; INSERT INTO Orders (CustomerID, Amount) VALUES (@val); SELECT * FROM inserted;

Common Mistakes

  • Mixing literals and parameters in dynamic SQL.

    Example: `EXEC ('SELECT * FROM Table WHERE ID = ' + @id)` fails because OUTPUT isn’t supported in generated queries. Use placeholders instead.

  • Using OUTPUT in a transaction without a matching INSERT/UPDATE.

    OUTPUT requires a preceding INSERT/UPDATE. This is impossible in a SELECT-only query.

  • Assuming parameterized queries work the same everywhere.

    Some SQL Server constructs (e.g., UDFs, CLR integration) may ignore OUTPUT. Test in a fresh query.

Tips

  • Use table variables or temp tables instead of OUTPUT.

    For dynamic scenarios, capture results in a temporary table: DECLARE @Temp TABLE (ID INT, Name NVARCHAR(100)); INSERT INTO @Temp VALUES (123, 'Test'); SELECT * FROM @Temp;

  • Validate with SELECT * FROM inserted.

    After INSERT/UPDATE, query `SELECT * FROM inserted` directly instead of OUTPUT. It’s more reliable.

  • Check the execution plan.

    Use `SET SHOWPLAN_TEXT ON` to see if the query optimizer skips OUTPUT. If it does, rewrite the plan.

  • Test in a non-transactional context first.

    Error 179 often surfaces in transactions. Temporarily drop `BEGIN TRANSACTION` to isolate the issue.

Troubleshooting

If the steps don’t resolve the error:

  1. Verify the Error 179 message.

    Error details may clarify why OUTPUT fails (e.g., "Operation not allowed with OUTPUT"). Check the full error log: SELECT ERROR_NUMBER(), ERROR_MESSAGE() FROM sys.fn_writestatements(1);

    If you see "*Operation not allowed with OUTPUT*," the query is not executing with the correct scope (e.g., missing INSERT/UPDATE).

  2. Switch to a stored procedure.

    If using dynamic SQL, rewrite the logic in a stored procedure with explicit parameters: CREATE PROCEDURE InsertOrder @CustomerID INT, @Amount DECIMAL(10,2) AS BEGIN INSERT INTO Orders (CustomerID, Amount) VALUES (@CustomerID, @Amount); END;

    Then call it statically with OUTPUT: EXEC InsertOrder 123, 1000 OUTPUT inserted.*;

  3. Use a try-catch block to debug.

    Wrap the query in a try-catch to isolate where it fails: BEGIN TRY -- Your problematic query here EXCEPT PRINT 'Error: ' + ERROR_MESSAGE();

    Identify the exact line causing the failure.

  4. Check for NULL or incompatible data types.

    Output operations require valid data. Test with a minimal example: INSERT INTO Products (ID) VALUES (123); SELECT * FROM inserted; If this fails, the issue might be a type mismatch or NULL handling.

  5. Try a different SQL Server version.

    If using SQL Server 2008, upgrade or use workarounds like temp tables. For older versions, avoid OUTPUT entirely unless necessary.

Frequently Asked Questions

Can I use OUTPUT with dynamic SQL?

No. Dynamic SQL (e.g., `EXEC('SELECT ...')`) bypasses SQL Server’s parameter handling, making OUTPUT impossible. Rewrite the query statically or use table variables instead.

Why does OUTPUT fail in a stored procedure with parameters?

Stored procedures with parameters typically execute as a single statement, not a dynamic query. OUTPUT only works in:

  • Direct queries (e.g., `INSERT ... OUTPUT`).
  • Transactions with matching INSERT/UPDATE.
Use `sp_executesql` *without* OUTPUT or query `SELECT * FROM inserted` instead.

How do I capture OUTPUT results without a transaction?

Use a table variable or temp table: DECLARE @Temp TABLE (ID INT); INSERT INTO @Temp VALUES (123); SELECT * FROM @Temp; This avoids transaction scope issues.

Is there a way to use OUTPUT with UDFs?

No. OUTPUT clauses are not supported in user-defined functions (UDFs) or CLR integrations. Capture results in a table variable or return a scalar value instead.

Why does SQL Server 2012+ still block OUTPUT in some cases?

Even newer versions may reject OUTPUT if:

  • The query is part of a bulk operation (e.g., `BULK INSERT`).
  • The OUTPUT targets an external system (e.g., a linked server).
  • The execution plan is modified by an optimizer rule.
Test with minimal queries or use `SELECT * FROM inserted` as a workaround.

Can I force SQL Server to allow OUTPUT for a specific user?

No. OUTPUT restrictions are system-wide. The only solutions are:

  • Modify the script to avoid OUTPUT.
  • Use table variables or temp tables.
Permissions don’t change this behavior.