I am looking to do something like this but it doesn’t compile. My stored proc returns a table. Here’s what I am trying to do – maybe someone can point to what I am doing wrong as this doesn’t compile:
MERGE table AS target
USING (EXEC [dbo].[sp_Something] @Rundate = '5/13/2011', @SPID = 56)
AS source (<Columns Returned By Stored Proc Go Here>)
ON TARGET.ID = SOURCE.ID
WHEN MATCHED THEN
UPDATE SET Field = Value...
WHEN NOT MATCHED THEN
INSERT ( Field )
VALUES (Value);
A stored procedure cannot be used where tables are expected. You must either use a table variable, subquery or a table-valued function. For example (not sure if this is valid, I’ve never used
MERGEbefore):