Den*_*nis 4 t-sql sql-server merge stored-procedures sql-server-2008
我希望做这样的事情,但它不编译.我的存储过程返回一个表.这就是我想要做的事情 - 也许有人可以指出我做错了,因为这不会编译:
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);
在期望表的地方不能使用存储过程.您必须使用表变量,子查询或表值函数.例如(不确定这是否有效,我以前从未使用MERGE过):
DECLARE @Something TABLE (columns go here...)
INSERT @Something
EXEC [dbo].[sp_Something] @Rundate = '5/13/2011', $SPID = 56
MERGE table as target
    USING @Something
       AS Source ...