未解决的错误代码

时间:2017-05-22 18:49:01

标签: sql-server

我附加了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行
  '附近的语法不正确('   Msg 137,Level 15,State 2,Line 8
  必须声明标量变量" @ din"。
  Msg 137,Level 15,State 2,Line 19
  必须声明标量变量" @ 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
  ')'附近的语法不正确。

非常感谢帮助。

1 个答案:

答案 0 :(得分:1)

首先:go之后你begin return(。{/ p>

第二:你正在编写函数,好像它是一个内联表值函数,但是你已经声明了一个标量返回。

不试图解开你在这里所拥有的东西(虽然看起来可能有更好的方法来做你想要做的事情):

这将更接近内联表值函数:

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

并会像这样使用:

select Id, Final
from dbo.fn_JointCreNotCk(@din,@jin) jcnc

参考: