SQL Server 2014 – DELETE with OUTPUT

sql servert-sql

Why would I get an 'incorrect syntax' error with this (on the OUTPUT keyword):

DELETE tb
    FROM dbo.TransactionBatch tb
    OUTPUT Deleted.TransactionID INTO #TransactionIDs
    INNER JOIN @BatchTransactionIDs tid 
        ON tb.TransactionID = tid.TransactionID

@BatchTransactionIDs is a User-Defined Table type

Best Answer

The OUTPUT has to go before the FROM.

DELETE tb
    OUTPUT Deleted.TransactionID INTO #TransactionIDs
    FROM dbo.TransactionBatch tb
    INNER JOIN @BatchTransactionIDs tid 
        ON tb.TransactionID = tid.TransactionID
Related Question