我希望你能对SQL Server提供相当新的帮助,因此请耐心等待。我正在尝试从CASE语句设置变量,但是遇到了多重标识符错误。代码试图根据两个日期字段的日期范围内的日期变量将值解析为变量。
use KSSDB
declare @DateFrom as datetime,
@DateTo as datetime,
@ResInt as int,
@ProgID as int,
@PERSID as int
set @DateFrom = '2010/11/01'
set @DateTo = '2013/11/01'
set @ProgID = 63
set @PERSID = 4492
--select PT.PROG_ID,
-- PT.PERS_ID,
-- P.PROG_NAME,
-- format(PT.PROG_TRAI_SD, 'dd/MM/yyyy') as PROG_TRAI_SD,
-- format(PT.PROG_TRAI_ED, 'dd/MM/yyyy')as PROG_TRAI_ED,
set @resint = CASE when (@DateFrom > PT.PROG_TRAI_SD and @dateto < PT.PROG_TRAI_ED) then 1
when (@datefrom > PT.PROG_TRAI_ED and @DateTo > PT.PROG_TRAI_ED) then 2
when (@datefrom < PT.PROG_TRAI_SD and @DateTo > PT.PROG_TRAI_ED) then 3
else 0
end
select @ResInt
from PROG p
JOIN PROG_TRAI PT ON P.PROG_ID = PT.PROG_ID
where PT.PROG_ID = @ProgID and
PT.PERS_ID = @PERSID
答案 0 :(得分:2)
问题是,您的CASE
声明与SELECT ... FROM
分开,并且您正尝试使用表格中的数据设置@resint
。
尝试使用以下内容:
select @resint
= CASE when (@DateFrom > PT.PROG_TRAI_SD and @dateto < PT.PROG_TRAI_ED) then 1
when (@datefrom > PT.PROG_TRAI_ED and @DateTo > PT.PROG_TRAI_ED) then 2
when (@datefrom < PT.PROG_TRAI_SD and @DateTo > PT.PROG_TRAI_ED) then 3
else 0
end
from PROG p
JOIN PROG_TRAI PT
ON P.PROG_ID = PT.PROG_ID
where PT.PROG_ID = @ProgID and
PT.PERS_ID = @PERSID