2013-08-16 59 views
0

我繼承了用戶定義的函數我試圖修改並遇到一些麻煩。我想將其修改爲常規查詢,然後將其轉化爲視圖。該功能是ufn_B2H_Enrolled_Clients_List。此功能可在選定的時間段內提取登記在程序中的客戶端列表。 ufn_B2H_Enrolled_Clients_List是函數ufn_B2H_STATUSES_THROUGH_DT。這將選擇客戶在所選時間範圍內所進行的最大狀態更改,如果客戶已註冊,然後取消註冊並再次註冊,則該功能不會將其用於計數。將用戶定義的函數修改爲視圖

我想什麼是採取ufn_B2H_Enrolled_Clients_List與看起來像這樣的輸出使之成爲一個常規查詢/視圖:

Total_Clients_Enrolled  Month Year 
252       1  2013 
247       2  2013 
303       3  2013 




**ufn_B2H_Enrolled_Clients_List Function** 

RETURNS varchar(100) 
AS 
BEGIN 
     -- Declare the return variable here 
     DECLARE @VALUE varchar(100) 
     SELECT @VALUE = VALUE 
     FROM ENUMS 
     WHERE NAME = @NAME AND NUMBER = @NUMBER 
     IF @VALUE IS NULL 
      SET @VALUE = 'unknown' 
     RETURN (@VALUE) 
END 

GO 


CREATE FUNCTION [dbo].[ufn_B2H_Enrolled_Clients_List](@From_DT datetime, @To_DT datetime) 
RETURNS @retB2H_Enrolled_Clients_List TABLE 
(
    CLT_NBR int 
) 
AS 
BEGIN 
     INSERT INTO @retB2H_Enrolled_Clients_List 
    --Include all clients enrolled prior to the period end date 


     SELECT CLT_NBR 
     FROM ECMS.dbo.ufn_B2H_STATUSES_THROUGH_DT(@To_DT) s1 
     WHERE s1.B2H_STATUS=4 AND s1.Max_Effect_DT <DATEADD(d,1,@To_DT) 
      --Exclude all clients disenrolled or transferred out prior to the start date 
      EXCEPT 
      SELECT s2.CLT_NBR 
      FROM ECMS.dbo.ufn_B2H_STATUSES_THROUGH_DT(@To_DT) s2 JOIN ECMS.dbo.ufn_B2H_STATUSES_THROUGH_DT(@To_DT) s1 ON s1.CLT_NBR=s2.CLT_NBR 
      WHERE s2.B2H_STATUS IN (7,9) AND s2.Max_Effect_DT <@From_DT AND s2.Max_Effect_DT>s1.Max_Effect_DT AND s1.B2H_STATUS IN (4,8) 
        --Exclude all clients who transferred in after the prior end date. 
        EXCEPT 
        SELECT s3.CLT_NBR 
        FROM ECMS.dbo.B2H_STATUS s3 JOIN ECMS.dbo.ufn_B2H_STATUSES_THROUGH_DT(@To_DT) s1 ON s1.CLT_NBR=s3.CLT_NBR 
        WHERE s3.B2H_STATUS = 8 AND s1.B2H_STATUS IN (4,8) AND s3.EFFECT_DT > @To_DT AND s3.Effect_DT>s1.Max_Effect_DT 

RETURN 
END 

GO 

CREATE FUNCTION [dbo].[ufn_B2H_STATUSES_THROUGH_DT](@Through_DT datetime) 
RETURNS @retB2H_STATUSES_THROUGH_DT TABLE 
(
    CLT_NBR int 
    ,B2H_STATUS int 
    ,Max_EFFECT_DT datetime 
) 
AS 
BEGIN 
    INSERT INTO @retB2H_STATUSES_THROUGH_DT 
     S 


     ELECT CLT_NBR 
       ,B2H_STATUS 
       ,MAX(EFFECT_DT) AS Max_Effect_DT 
      FROM [ECMS].[dbo].[B2H_STATUS] 
      WHERE EFFECT_DT<DATEADD(d,1,@Through_DT) 
      GROUP BY CLT_NBR, B2H_STATUS 

RETURN 
END 

GO 

回答

0

因爲dbo.ufn_B2H_Enrolled_Clients_List函數的參數,你可以把它轉換爲嵌入UDF從而:

CREATE FUNCTION [dbo].[ufn_B2H_Enrolled_Clients_List](@From_DT datetime, @To_DT datetime) 
RETURNS TABLE 
AS 
RETURN 
     SELECT CLT_NBR 
     FROM ECMS.dbo.ufn_B2H_STATUSES_THROUGH_DT(@To_DT) s1 
     WHERE s1.B2H_STATUS=4 AND s1.Max_Effect_DT <DATEADD(d,1,@To_DT) 
      --Exclude all clients disenrolled or transferred out prior to the start date 
      EXCEPT 
      SELECT s2.CLT_NBR 
      FROM ECMS.dbo.ufn_B2H_STATUSES_THROUGH_DT(@To_DT) s2 JOIN ECMS.dbo.ufn_B2H_STATUSES_THROUGH_DT(@To_DT) s1 ON s1.CLT_NBR=s2.CLT_NBR 
      WHERE s2.B2H_STATUS IN (7,9) AND s2.Max_Effect_DT <@From_DT AND s2.Max_Effect_DT>s1.Max_Effect_DT AND s1.B2H_STATUS IN (4,8) 
        --Exclude all clients who transferred in after the prior end date. 
        EXCEPT 
        SELECT s3.CLT_NBR 
        FROM ECMS.dbo.B2H_STATUS s3 JOIN ECMS.dbo.ufn_B2H_STATUSES_THROUGH_DT(@To_DT) s1 ON s1.CLT_NBR=s3.CLT_NBR 
        WHERE s3.B2H_STATUS = 8 AND s1.B2H_STATUS IN (4,8) AND s3.EFFECT_DT > @To_DT AND s3.Effect_DT>s1.Max_Effect_DT 

GO 

內聯UDF是帶有參數的視圖:

內聯函數可以用來實現參數化視圖的功能 。

參考文獻:http://technet.microsoft.com/en-us/library/aa214762(v=sql.80).aspx

相關問題