我有3个主班
08:00〜16:00是S1
16:00〜24:00是S2
00:00〜08:00是S3
我必须基于上面的3个偏移获取所有数据,现在我正在创建一个基于3班的存储过程来检索所有数据。
ALTER PROCEDURE sp_shift
(
@V2_WORK_DATE DATETIME,
@V3_SHIFT_NO VARCHAR(2)
)
AS
DECLARE @shift VARCHAR(5);
select t3.scn,t3.vsl_name, t1.qty, t1.opr_type, t1.hatch_num,t1.opr_st_dt_tm,t1.opr_ed_dt_tm,t1.wght,t1.vol,t2.description,t3.disc_load,
case when @V3_SHIFT_NO = 'S1' THEN DATEPART(HH,@V2_WORK_DATE)> = 8 AND DATEPART(HH, @V2_WORK_DATE) <16
when @V3_SHIFT_NO = 'S2' THEN DATEPART(HH,@V2_WORK_DATE)> = 16 AND DATEPART(HH, @V2_WORK_DATE) <24
when @V3_SHIFT_NO = 'S1' THEN DATEPART(HH,@V2_WORK_DATE)> = 8 AND DATEPART(HH, @V2_WORK_DATE) <16 end as @shift
from ccostsitem t1
inner join ccoscargotype t2 on t2.code = t1.cg_type_id
inner join ccostallysheet t3 on t3.id = t1.master_id
where @shift = @V3_SHIFT_NO
以下是我如何使用param
执行SPEXEC sp_shift '2016-12-30 08:00:00.000','S1'
此存储过程的问题是case when
显示错误接近END提示,即' incorrect syntax near end
'
何时要评估条件,则是结论。
SELECT *
FROM (SELECT t3.scn
, t3.vsl_name
, t1.qty
, t1.opr_type
, t1.hatch_num
, t1.opr_st_dt_tm
, t1.opr_ed_dt_tm
, t1.wght
, t1.vol
, t2.description
, t3.disc_load
, CASE WHEN DATEPART(HH,@V2_WORK_DATE)> = 8 AND DATEPART(HH, @V2_WORK_DATE) <16 THEN 'S1'
WHEN DATEPART(HH,@V2_WORK_DATE)> = 16 AND DATEPART(HH, @V2_WORK_DATE) <24 THEN 'S2'
WHEN DATEPART(HH,@V2_WORK_DATE)> = 8 AND DATEPART(HH, @V2_WORK_DATE) <16 THEN 'S3' END AS Shift
FROM ccostsitem t1
INNER JOIN ccoscargotype t2 on t2.code = t1.cg_type_id
INNER JOIN ccostallysheet t3 on t3.id = t1.master_id
) a
WHERE Shift = @V3_SHIFT_NO
您要将变量=设置为案例语句,而不是使用。正如别名命令一样,并期望一列(在这种情况下(。这应该有效:
@shift = case when @V3_SHIFT_NO = 'S1' THEN DATEPART(HH,@V2_WORK_DATE)> = 8 AND DATEPART(HH, @V2_WORK_DATE) <16
when @V3_SHIFT_NO = 'S2' THEN DATEPART(HH,@V2_WORK_DATE)> = 16 AND DATEPART(HH, @V2_WORK_DATE) <24
when @V3_SHIFT_NO = 'S1' THEN DATEPART(HH,@V2_WORK_DATE)> = 8 AND DATEPART(HH, @V2_WORK_DATE) <16 end
编辑:实际上,在第二次审查时,您的查询可能需要一些其他帮助,但希望您可以朝正确的方向前进。
不要使用 @ sign:
来自:
end as @shift
to:
end as shift