I have a table that will hold pending data that will update another table on the same server. This pending table gets it data from another servers stored procedure. Once data is in this pending table, a stored procedure will attempt to insert, update, or delete based on a tinyint column in the pending table.
This pending table will remain empty unless something needs to occur. At the end of the procedure, we delete from this pending table.
An issue arose by way of what happens if the same record that needs to be updated gets inserted twice from the other server while the procedure is running. At the end, both records would be cleared. I’ve attempted to solve this by adding an incremental id onto the pending table but I can’t output the id in the insert:
insert into config.table (col1, col2, col3)
output inserted.col3, pendingId into @table
select p.col1, p.col2, p.col3
from pendingTable p
left join config.table t on p.col1 = t.col1
where t.col1 is null
I’m getting an error on the pendingId. I plan to use the @table variable to delete from pending to avoid deleting records that were not touched (newly inserted records that were added while procedure was running).
delete p
from pendingTable p
inner join @table t on p.pendingId = t.pendingId and p.col3 = t.col3
You need to cache the pending records and delete them after.
ORIGINAL ANSWER BELOW
You’re missing
insertedbeforependingid, i.e.inserted.pendingid. It’s to disambiguate betweeninsertedanddeleted.The following works.
Oh, and you’re missing the entire SELECT statement as well, or I’m assuming it’s actually due to creating the “mock” tables for the question?