2017-06-01 84 views
2

我有一個表函數,它需要2個參數並返回2列。爲了簡便起見,讓調用函數Get_GroupNumber需要調用表函數

Get_GroupNumber('Smith', '123-456-789') 

這將返回2列,再次,保持簡單說,這是Reason1和Reason2現在

Reason1 = '111-222-333' 
Reason2 = '555'666'777' 

,我需要做的是將Reason1返回到Get_GroupNumber,並繼續執行,直到它在Reason1中返回NULL,並在Reason2中返回NULL。

所以,第二個電話應該是這樣

Get_GroupNumber('Smith','111-222-333') 

有沒有辦法做到這一點?我使用SQL Server提前

+1

1.該函數返回NULL? 2.爲什麼你不能使用'While'循環?爲了警告,這聽起來像它可能是一個無盡的循環。 –

+0

我無法編輯該功能本身。是的,該函數可以返回NULL。雖然Loop聽起來不錯,但我會嘗試查找如何做到這一點(對不起,SQL編程新手) – NullPointer

+0

好的。我正在談論調用函數的存儲過程(如果有的話)。在這個循環中。 –

回答

3

謝謝如果你不想使用迭代的方法(例如,具有while循環),那麼遞歸CTE可以做的工作。你可以閱讀更多有關遞歸CTE的here,或看看這個例子:

-- Here's a "dummy" function that has some hard-coded return values for illustrative 
-- purposes. It will return ('111-222-333', '555-666-777') when it gets 123-456-789 
-- as its second parameter, ('999-999-999', '000-000-000') when it gets 111-222-333 
-- as its second parameter, and (null, null) otherwise. 
create function Get_GroupNumber 
( 
    @Param1 varchar(32), 
    @Param2 varchar(32) 
) 
returns table 
as 
return 
(
    select 
     Reason1 = case @Param2 when '123-456-789' then '111-222-333' when '111-222-333' then '999-999-999' else null end, 
     Reason2 = case @Param2 when '123-456-789' then '555-666-777' when '111-222-333' then '000-000-000' else null end 
); 
go 

-- The sample inputs from your question. 
declare @Param1 varchar(32) = 'Smith'; 
declare @Param2 varchar(32) = '123-456-789'; 

-- And finally, the good stuff: 
with GroupNumberCTE as 
(
    -- Base case: pass the original parameters, get the results from the function, and 
    -- create a new field called "Call #" that will illustrate the order in which the 
    -- various calls to Get_GroupNumber were made. (This field is purely informational; 
    -- you can remove it if you don't want it.) 
    select 
     [Call #] = 1, 
     Reason1, 
     Reason2 
    from 
     Get_GroupNumber(@Param1, @Param2) 

    union all 

    -- Recursive case: if the previous call returned a non-null value in either field, 
    -- invoke the function again with the original @Param1 and the Reason1 from the 
    -- previous call. 
    select 
     [Call #] = [Previous].[Call #] + 1, 
     [Next].Reason1, 
     [Next].Reason2 
    from 
     GroupNumberCTE [Previous] 
     cross apply Get_GroupNumber(@Param1, [Previous].Reason1) [Next] 
    where 
     [Previous].Reason1 is not null or 
     [Previous].Reason2 is not null 
) 
select * from GroupNumberCTE; 

結果集:

Call # Reason1  Reason2 
---------------------------------- 
1  111-222-333 555-666-777 
2  999-999-999 000-000-000 
3  NULL   NULL 

我應該指出的是,這裏有一個危險:沒有什麼結構我的CTE本身保證遞歸最終會結束。因此,您必須確保對於任何可行的初始輸入集合,您實施Get_GroupNumber最終將返回兩個空值。只要是這樣,這種方法應該運作良好。

+0

PErfect,謝謝! :) – NullPointer

0
Using While Loop 

DECLARE @key varchar(max); 
DECLARE @retVal varchar(max); 
SET @key = '123-456-789' 
While @Key is not null 
BEGIN 
    set @retval = @key 
    SET @key = (SELECT Return2 from [dbo].[Get_GroupNumber]('Smith',@key)) 
END; 

print @retVal 

我知道,我只使用從柱RETURN2的返回值來這裏測試

相關問題