0

请在下面查看我的存储过程。我试图从 CASE 语句中的参数传递值 1 或 0。

我得到的错误是在 SELECT 语句中调用 CASE 语句中的参数 @payment_status :

消息 102,级别 15,状态 1,第 17 行 '=' 附近的语法不正确。

消息 102,级别 15,状态 1,第 21 行 '=' 附近的语法不正确。

这是脚本:

ALTER PROC ash_ap_cash_req_sp (@End_Date     NVARCHAR(12),
                               @Payment_Hold NVARCHAR(32))
AS
    SET NOCOUNT ON

    DECLARE @debug           INT,
            @julian_end_date VARCHAR(7),
            @ap_cash_req     NVARCHAR(4000),
            @payment_status  VARCHAR(400)

    SET @End_Date = RIGHT('00' + CONVERT(VARCHAR(2), datepart(mm, @End_Date)), 2) + '/' + RIGHT('00' + CONVERT(VARCHAR(2), datepart(dd, @End_Date)), 2) + '/' + RIGHT('0000' + CONVERT(VARCHAR(4), datepart(yyyy, @End_Date)), 4)
    SET @julian_end_date = (SELECT datediff(dd, '1/1/1753', @End_Date) + 639906)

    IF @Payment_Hold = 'All'
      BEGIN
          SET @payment_status = '
    SELECT payment_hold_flag 
    FROM dbase.dbo.apvchr 
    WHERE payment_hold_flag in (0,1)'
      END
    ELSE IF @Payment_Hold = 'Yes'
      BEGIN
          SET @payment_status = '
    SELECT payment_hold_flag 
    FROM dbase.dbo.apvchr 
    WHERE payment_hold_flag = 1'
      END
    ELSE IF @Payment_Hold = 'No'
      BEGIN
          SET @payment_status = '
    SELECT payment_hold_flag 
    FROM dbase.dbo.apvchr 
    WHERE payment_hold_flag = 0'
      END

    SET @debug = 0
    SET @ap_cash_req = '
IF OBJECT_ID(''temp.dbo.##ash_ap_cash_req_sp'') is NOT NULL
DROP TABLE ##ash_ap_cash_req_sp;  

CREATE TABLE ##ash_ap_cash_req_sp
(vendor varchar(12)
, pay_to varchar(12)
, vendor_name varchar(32)
, voucher_no varchar(12)
, invoice_no varchar(32)
, date_doc varchar(12)
--, date_doc_jd varchar(12)
, date_due  varchar(12)
--, date_due_jd  varchar(12)
, date_applied  varchar(12)
--, date_applied_jd  varchar(12)
, paid_status varchar(8)
--, payment_status varchar(12)
, payment_hold_status varchar(8)
, amount_open decimal(20,2)
, days_diff float
, days_0_to_30 decimal(20,2)
, days_31_to_60 decimal(20,2)
, days_61_to_90 decimal(20,2)
, days_91_to_120 decimal(20,2)
, days_121_to_150 decimal(20,2)
, total_over_90 decimal(20,2)
, total_over_150 decimal(20,2)
, payment_code varchar(8)
)'

    IF @debug = 1
      BEGIN
          PRINT @ap_cash_req

          PRINT ''
      END
    ELSE
      BEGIN
          EXEC sp_executesql @ap_cash_req
      END

    SET @ap_cash_req = '
SELECT
m.vendor
, m.pay_to
, m.address_name as vendor_name
, h.trx_ctrl_num as voucher_no
, h.doc_ctrl_num as invoice_no
, CONVERT(varchar(12),dateadd(dd,(h.date_doc- 639906),''1/1/1753''),101) as date_doc
--, h.date_doc as date_doc_jd
, CONVERT(varchar(12),dateadd(dd,(h.date_due- 639906),''1/1/1753''),101) as date_due
--, h.date_due as date_due_jd
, CONVERT(varchar(12),dateadd(dd,(h.date_applied- 639906),''1/1/1753''),101) as date_applied
--, h.date_applied as date_applied_jd
, paid_status = CASE WHEN h.paid_flag = 1 
    THEN ''Paid''
    ELSE ''Not Paid'' END 
, h.payment_hold_flag = CASE 
    WHEN ' + @payment_status + ' = 1 THEN ''On Hold''
    ELSE ''Not On Hold''
    END as payment_hold_status
, amount_open = (amt_net-amt_paid_to_date)
, h.payment_code
, days_dif = (' + @julian_end_date + ' - h.date_applied)
, days_0_to_30 = CASE WHEN h.date_applied >= ' + @julian_end_date + '-30
            THEN h.amt_net  --(h.amt_net-h.amt_paid_to_date)
            ELSE 0 END
, days_31_to_60 = CASE WHEN h.date_applied >= ' + @julian_end_date + '-60 
            AND h.date_applied <= ' + @julian_end_date + '-31
            THEN h.amt_net
            ELSE 0 END
, days_61_to_90 = CASE WHEN h.date_applied >= ' + @julian_end_date + '-90
            AND h.date_applied <= ' + @julian_end_date + '-61
            THEN h.amt_net
            ELSE 0 END
, days_91_to_120 = CASE WHEN h.date_applied >= ' + @julian_end_date + '-120
            AND h.date_applied <= ' + @julian_end_date + '-91
            THEN h.amt_net
            ELSE 0 END
, days_121_to_150 = CASE WHEN h.date_applied >= ' + @julian_end_date + '-150
            AND h.date_applied <= ' + @julian_end_date + '-121
            THEN h.amt_net
            ELSE 0 END
, total_over_90 = CASE WHEN h.date_applied <= ' + @julian_end_date + '-91
            THEN h.amt_net
            ELSE 0 END      
, total_over_150 = CASE WHEN h.date_applied <= ' + @julian_end_date + '-151
            THEN h.amt_net
            ELSE 0 END
FROM dbase.dbo.apvchr h
JOIN dbase.dbo.apvdet d
ON h.trx_ctrl_num = d.trx_ctrl_num
JOIN dbase.dbo.master m
ON h.vendor = m.vendor
AND h.pay_to = m.pay_to
GROUP BY m.vendor, m.pay_to, m.address_name
, h.trx_ctrl_num, h.doc_ctrl_num, h.date_doc, h.date_due, h.date_applied, h.paid_flag, h.payment_hold_flag
, h.amt_net, h.amt_paid_to_date, h.payment_code, h.payment_hold_flag 
--ORDER BY NEWID()
'

    IF @debug = 1
      BEGIN
          PRINT @ap_cash_req

          PRINT ''
      END
    ELSE
      BEGIN
          EXEC sp_executesql @ap_cash_req
      END

    SET @ap_cash_req = '
SELECT * FROM ##ash_ap_cash_req_sp
';
    SET @ap_cash_req = '
DROP TABLE ##ash_ap_cash_req_sp
';

    IF @debug = 1
      BEGIN
          PRINT @ap_cash_req

          PRINT ''
      END
    ELSE
      BEGIN
          EXEC sp_executesql @ap_cash_req
      END 

EXEC dbase.dbo.ash_ap_cash_req_sp;1 '03/11/2013','Yes'
4

1 回答 1

2

因为@payment_status 是一个“SELECT ...”,所以您需要用大括号将其包裹起来

h.payment_hold_flag = CASE 
  WHEN ('+@payment_status+') = 1 THEN ''On Hold''
  ELSE ''Not On Hold''
END as payment_hold_status

但是,除此之外,我会质疑语法:
, amount_open = (amt_net-amt_paid_to_date)
- 你的意思是
, (amt_net-amt_paid_to_date) as amount_open

于 2013-03-12T21:32:54.417 回答