将SQL语句转换为变量的情况

时间:2016-06-23 14:48:05

标签: tsql sybase-ase

我需要在sybase sql上为case when statement创建一个变量。我的SQL客户端出现此错误

  

为变量赋值的SELECT语句不能是   结合数据检索操作。

以正确的方式使用此变量需要做什么?

DECLARE @OutputName CHAR(50) 
SELECT @OutputName=
(case when (a.M_TRN_TYPE='XSW' or a.M_TRN_TYPE='SWLEG') then 'FXSW'
      when (a.M_TRN_TYPE<>'SWLEG' and a.M_TP_DVCS='C') then 'DCS'
      when a.M_TRN_TYPE<>'SWLEG' and a.M_TP_DVCS<>'C' and SUBSTRING(c.M_SP_SCHED0,1,2)='+1' then 
                                                                                      (case when b.M_DTE_SKIP_1>=a.M_TP_DTEEXP then 'SPOT' else 'OUTR' end)
      when a.M_TRN_TYPE<>'SWLEG' and a.M_TP_DVCS<>'C' and SUBSTRING(c.M_SP_SCHED0,1,2)<>'+1' then 
                                                                                      (case when b.M_DTE_SKIP_2>=a.M_TP_DTEEXP then 'SPOT' else 'OUTR' end)
end),
case when @OutputName='XSW' and 
                (case when c.M_DATESKIP='+1OD' then b.M_DTE_SKIP_1 else b.M_DTE_SKIP_2 end)=a.M_TP_DTEFST 
                      then 0 
                 else a.M_NB
                 end as 'NBI'
from TP_COMPL_PL_REP b
join TP_ALL_REP a on (a.M_NB=b.M_NB and a.M_REF_DATA=b.M_REF_DATA) 
left join DM_SPOT_CONTRACT_REP c on (a.M_NB=c.M_NB and a.M_REF_DATA=c.M_REF_DATA) 

1 个答案:

答案 0 :(得分:0)

问题在于,您无法将设置变量的SELECT与在屏幕上返回数据的SELECT合并。

您将@OutputName设置为(case when (a.M_TRN_TYPE='XSW' .. ),但在同一SELECT中,您还尝试显示以下结果:

case
    when @OutputName='XSW' and 
         (case 
              when c.M_DATESKIP='+1OD' 
                  then b.M_DTE_SKIP_1 
              else b.M_DTE_SKIP_2 
          end)=a.M_TP_DTEFST 
              then 0 
    else a.M_NB
end as 'NBI'

您不能以您使用的格式在同一个SELECT中拥有这两个。我的建议是拆分它们,一个用于分配变量:

DECLARE @OutputName CHAR(50) 
SELECT @OutputName=
(case when (a.M_TRN_TYPE='XSW' or a.M_TRN_TYPE='SWLEG') then 'FXSW'
      when (a.M_TRN_TYPE<>'SWLEG' and a.M_TP_DVCS='C') then 'DCS'
      when a.M_TRN_TYPE<>'SWLEG' and a.M_TP_DVCS<>'C' and SUBSTRING(c.M_SP_SCHED0,1,2)='+1' then 
                                                                                      (case when b.M_DTE_SKIP_1>=a.M_TP_DTEEXP then 'SPOT' else 'OUTR' end)
      when a.M_TRN_TYPE<>'SWLEG' and a.M_TP_DVCS<>'C' and SUBSTRING(c.M_SP_SCHED0,1,2)<>'+1' then 
                                                                                      (case when b.M_DTE_SKIP_2>=a.M_TP_DTEEXP then 'SPOT' else 'OUTR' end)
end)
from TP_COMPL_PL_REP b
join TP_ALL_REP a on (a.M_NB=b.M_NB and a.M_REF_DATA=b.M_REF_DATA) 
left join DM_SPOT_CONTRACT_REP c on (a.M_NB=c.M_NB and a.M_REF_DATA=c.M_REF_DATA)

和一个用于返回数据:

SELECT case when @OutputName='XSW' and 
                (case when c.M_DATESKIP='+1OD' then b.M_DTE_SKIP_1 else b.M_DTE_SKIP_2 end)=a.M_TP_DTEFST 
                      then 0 
                 else a.M_NB
                 end as 'NBI'
from TP_COMPL_PL_REP b
join TP_ALL_REP a on (a.M_NB=b.M_NB and a.M_REF_DATA=b.M_REF_DATA) 
left join DM_SPOT_CONTRACT_REP c on (a.M_NB=c.M_NB and a.M_REF_DATA=c.M_REF_DATA)