您可以在SQL Server從T-SQL函數返回一個表2008在SQL Server返回多個表從T-SQL函數2008
我想知道是否有可能返回多個表。
這種情況是我有三個查詢過濾3個不同的表。每個表格都會根據我想從函數返回的5個過濾表格進行過濾;而不是在每個查詢中複製並粘貼其創建。
的是什麼,這將看起來像複製和粘貼一個簡單的例子:
FUNCTION GetValuesA(@SomeParameter int) RETURNS @ids TABLE (ID int) AS
WITH Filter1 As (Select id FROM FilterTable1 WHERE Attribute=SomeParameter)
, Filter2 As (Select id FROM FilterTable2 WHERE Attribute=SomeParameter)
INSERT INTO @IDs
SELECT ID FROM ValueTableA
WHERE ColA IN (SELECT id FROM Filter1)
AND ColB IN (SELECT id FROM Filter2)
RETURN
-----------------------------------------------------------------------------
FUNCTION GetValuesB(@SomeParameter int) RETURNS @ids TABLE (ID int) AS
WITH Filter1 As (Select id FROM FilterTable1 WHERE Attribute=SomeParameter)
, Filter2 As (Select id FROM FilterTable2 WHERE Attribute=SomeParameter)
INSERT INTO @IDs
SELECT ID FROM ValueTableB
WHERE ColA IN (SELECT id FROM Filter1)
AND ColB IN (SELECT id FROM Filter2)
AND ColC IN (SELECT id FROM Filter2)
RETURN
所以,這兩個查詢之間的唯一區別是被過濾的表,如何(Where子句)。
我想知道我是否可以從函數返回Filter1 & Filter2。我也樂於提出解決這個問題的不同方法。
您不能從同一過程返回兩個不同的結果集。如果你要處理結果,那麼兩個查詢都要考慮UNION,INTERSECT或MINUS。 –