2017-05-22 76 views
0

我附上一個CTE的代碼,它可以正常工作。我試圖從一個函數內運行它,我從來沒有做過,並得到我簡單可以修復的錯誤。更多的關注這個問題將不勝感激。我不覺得有必要解釋CTE代碼,因爲它可以很好地工作,問題在於它被描述爲一個函數。下面是代碼和SQL Server 2008 R2的錯誤消息:未解決的錯誤代碼

USE Med 
Go 

SET ANSI_NULLS ON 
GO 

SET QUOTED_IDENTIFIER ON 
GO 

-- ============================================= 
-- Author:  Robert G. Seminario 
-- Create date: 5/19/2017 
-- Description: Joint Credit Not Checked 
-- ============================================= 
CREATE FUNCTION [dbo].[fn_JointCreNotCk] 

    (@din as Nvarchar (40), @jin as Nvarchar(30)) -- Pass in Donor_ID_Number and Joint_ID_Number 

RETURNS Nvarchar(40) 

AS 
BEGIN 
Return 
(

Go 

    With dn_cte (TRANSACTION_ID, Donor_ID_Number, ID_Number, DonorFound) 

    AS 
(
     Select 
     olg.TRANSACTION_ID, olg.Donor_ID_Number, lmc.Id_Number, 
     (CASE When lmc.Id_Number = @din THEN '1' ELSE '0' END) as DonorFound 
     from OnlineGivingDailyLoad as olg 
     left outer join LMC_ENTITY as lmc 
     on lmc.Id_Number = olg.Donor_ID_Number 

), 
Jt_cte as 

(
     Select 
     olg.TRANSACTION_ID, olg.Joint_ID_Number, lmc.Id_Number, 
     CASE When lmc.Id_Number = @jin THEN '1' ELSE '0' END As JointFound 
     from OnlineGivingDailyLoad as olg 
     left outer join LMC_ENTITY as lmc 
     on lmc.Id_Number = olg.Joint_ID_Number 

), 
    dnjnt_cte as 

(

     Select 
     olg.TRANSACTION_ID, olg.Donor_ID_Number, 
     (CASE WHEN e.jnt_Gifts_ind = 'Y' THEN '1' ELSE '0' END)as D_JntGI 
     from OnlineGivingDailyLoad as olg 
     left outer join Advance.dbo.ENTITY as e 
     on e.ID_NUMBER = olg.Donor_ID_Number 


), 
    Jtjnt_cte as 

(

     Select 
     olg.TRANSACTION_ID, olg.Joint_ID_Number, 
     (CASE WHEN e.jnt_Gifts_ind = 'Y' THEN '1' ELSE '0' END)as J_JntGI 
     from OnlineGivingDailyLoad as olg 
     left outer join Advance.dbo.ENTITY as e 
     on e.ID_NUMBER = olg.Joint_ID_Number 
), 
    JtCkNotCk as 

(
     Select dn_cte.TRANSACTION_ID, 
     (Case WHEN cast(dn_cte.DonorFound as int) + cast(Jt_cte.JointFound as int) = 2 THEN 1 ELSE 0 END)as BothFound, 
     (CASE WHEN cast(dnjnt_cte.D_JntGI as int) + cast(Jtjnt_cte.J_JntGI as int) = 2 THEN 1 ELSE 0 END)as JointG 
     from dn_cte 
     inner join jt_cte 
     on Jt_cte.TRANSACTION_ID = dn_cte.TRANSACTION_ID 
     inner join dnjnt_cte 
     on dnjnt_cte.TRANSACTION_ID = dn_cte.TRANSACTION_ID 
     inner join Jtjnt_cte 
     on Jtjnt_cte.TRANSACTION_ID = dn_cte.TRANSACTION_ID 
) 

Select TRANSACTION_ID, (CASE WHEN (BothFound + JointG) = 2 THEN 1 ELSE 0 END) AS Final 
from JtCkNotCk 
order by 1 

) 

END 

Go 

現在的錯誤信息:

消息102,級別15,狀態1,過程fn_JointCreNotCk,行16
附近有語法錯誤'('。
消息137,級別15,狀態2,行8
必須聲明標量變量 「@din」。
消息137,級別15,狀態2,行19
必須聲明第e標量變量「@jin」。
Msg 102,Level 15,State 1,Line 37
','附近的語法不正確。
Msg 102,Level 15,State 1,Line 48
','附近語法不正確。
Msg 102,Level 15,State 1,Line 68
')'附近的語法不正確。

幫助表示讚賞。

+0

側面說明,請不要使用1'爲了' - 指定實際列名 – Adam

回答

1

第一條:您在begin return(之後go

第二:您正在編寫函數,就好像它是內聯表值函數一樣,但您已聲明瞭標量返回。

沒有試圖解開你有什麼在這裏(雖然它看起來像有可能是一個更好的方式做你正在嘗試做的):

這將是更接近內嵌表值函數:

use Med go 
set ansi_nulls, quoted_identifier on; 
go 
-- ============================================= 
-- Author: Robert G. Seminario 
-- Create date: 5/19/2017 
-- Description: Joint Credit Not Checked 
-- ============================================= 
-- Pass in Donor_ID_Number and Joint_ID_Number 
CREATE FUNCTION [dbo].[fn_JointCreNotCk] (@din as nvarchar(40), @jin as nvarchar(30)) 
returns table as return (
    with dn_cte(TRANSACTION_ID, Donor_ID_Number, ID_Number, DonorFound) as (
    select 
     olg.TRANSACTION_ID 
     , olg.Donor_ID_Number 
     , lmc.Id_Number 
     , (case when lmc.Id_Number = @din then 1 else 0 end) as DonorFound 
    from OnlineGivingDailyLoad as olg 
     left join LMC_ENTITY as lmc on lmc.Id_Number = olg.Donor_ID_Number 
    ) 
    , Jt_cte as (
    select olg.TRANSACTION_ID 
     , olg.Joint_ID_Number 
     , lmc.Id_Number 
     , case when lmc.Id_Number = @jin then 1 else 0 end as JointFound 
    from OnlineGivingDailyLoad as olg 
     left join LMC_ENTITY as lmc on lmc.Id_Number = olg.Joint_ID_Number 
    ) 
    , dnjnt_cte as (
    select olg.TRANSACTION_ID 
     , olg.Donor_ID_Number 
     , (case when e.jnt_Gifts_ind = 'Y' then 1 else 0 end) as D_JntGI 
    from OnlineGivingDailyLoad as olg 
     left join Advance.dbo.ENTITY as e on e.ID_NUMBER = olg.Donor_ID_Number 
    ) 
    , Jtjnt_cte as (
    select olg.TRANSACTION_ID 
     , olg.Joint_ID_Number 
     , (case when e.jnt_Gifts_ind = 'Y' then 1 else 0 end) as J_JntGI 
    from OnlineGivingDailyLoad as olg 
     left join Advance.dbo.ENTITY as e on e.ID_NUMBER = olg.Joint_ID_Number 
    ) 
    , JtCkNotCk as (
    select dn_cte.TRANSACTION_ID 
     , (case when cast(dn_cte.DonorFound as int) + cast(Jt_cte.JointFound as int) = 2 then 1 else 0 end) as BothFound 
     , (case when cast(dnjnt_cte.D_JntGI as int) + cast(Jtjnt_cte.J_JntGI as int) = 2 then 1 else 0 end) as JointG 
     from dn_cte 
     inner join jt_cte on Jt_cte.TRANSACTION_ID = dn_cte.TRANSACTION_ID 
     inner join dnjnt_cte on dnjnt_cte.TRANSACTION_ID = dn_cte.TRANSACTION_ID 
     inner join Jtjnt_cte on Jtjnt_cte.TRANSACTION_ID = dn_cte.TRANSACTION_ID 
    ) 
    select TRANSACTION_ID 
    , (case when (BothFound + JointG) = 2 then 1 else 0 end) as Final 
    from JtCkNotCk 
    order by 1 
    ) 
go 

,並會使用像這樣:


參考:

+0

還要注意的是'go'不是一個t-sql命令,但它是各種實用程序的信號,他們應該執行前一批。它永遠不會出現在函數或存儲過程的主體中。 https://docs.microsoft.com/en-us/sql/t-sql/language-elements/sql-server-utilities-statements-go – Donnie

+0

感謝您的協助SQLZim,我真的把自己搞砸了標量vs 。基於CTE的表格。只是好奇,你提到要完成這個更好的方法是什麼? – SQLPadawan

+0

@RobSeminario這將需要看到你的三個表的一些示例數據和所需的結果。我基於我的假設,事實上,你有一個表的4x調用,並且在最終cte和隨後的查詢之前每個調用2x到另外兩個表。 – SqlZim