在存储过程结果上排除SQL Server

6
我想使用存储过程的结果来完成这个操作:
select * from table where column=whatever
except
select * from table2 where column=whatever

so

exec sp1 args
except
exec sp2 args

我的SPS没有返回值,它们只接受参数并返回选择语句的结果。


3
你可以使用存储过程填充临时表,也可以使用表值函数或视图。 - Tim Schmelter
我无法更改SPS,但可以尝试使用临时表。 - Rob Sedgwick
1个回答

6

试试这个。

CREATE PROCEDURE usp_sp1_Except_sp2
@sp1 args     --<-- All the params sp1 Expects
@sp2 args     --<-- All the params sp2 Expects
AS
BEGIN
  SET NOCOUNT ON;

IF OBJECT_ID('tempdb..#SP1_Results') IS NOT NULL
DROP TABLE #SP1_Results

IF OBJECT_ID('tempdb..#SP2_Results') IS NOT NULL
DROP TABLE #SP2_Results


CREATE TABLE #SP1_Results
(
  -- Define table structure here
)

CREATE TABLE #SP2_Results
(
  -- Define table structure here
)


INSERT INTO #SP1_Results
EXECUTE dbo.sp1 @sp1

INSERT INTO #SP2_Results
EXECUTE dbo.sp2 @sp2

SELECT * FROM #SP1_Results
EXCEPT 
SELECT * FROM #SP2_Results

  SET NOCOUNT OFF;

END

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接