sql 存储过程中 用case when 替代if else
我需要把:CreatePROCEDUREdbo.sp_getStatisticInfo(@IDnvarchar(50))ASif(@ID='1')beginselect*...
我需要把:
Create PROCEDURE dbo.sp_getStatisticInfo( @ID nvarchar(50) )
AS
if(@ID='1')
begin
select * from tb_dailyCheck where f_startTime between @StartTime and dateadd(day,1,@EndTime)
end
else if(@ID='2')
begin
select * from tb_shiftCheck where f_startTime between @StartTime and dateadd(day,1,@EndTime)
end
else
begin
select * from tb_weeklyM where f_startTime between @StartTime and dateadd(day,1,@EndTime)
end
改成这样:
Create PROCEDURE dbo.sp_getStatisticInfo( @ID nvarchar(50) )
AS
Case @ID
when '1' then
begin
select * from tb_dailyCheck where f_startTime between @StartTime and dateadd(day,1,@EndTime)
end
when '2' then
begin
select * from tb_shiftCheck where f_startTime between @StartTime and dateadd(day,1,@EndTime)
end
else
begin
select * from tb_weeklyM where f_startTime between @StartTime and dateadd(day,1,@EndTime)
end
前一段是可以的,但是后一段运行的时候总是语法错误,望大虾指点啊:-)
比如下面这段,也是跑不起来的:
Create procedure Test1(@Name varchar(50))
As
Case When @Name='s' Then (print 'DName')
When @Name='d' Then (print 'did')
Else (print 'dids')
End
去掉小括号同样跑不起来
望大虾指点一二。。。 展开
Create PROCEDURE dbo.sp_getStatisticInfo( @ID nvarchar(50) )
AS
if(@ID='1')
begin
select * from tb_dailyCheck where f_startTime between @StartTime and dateadd(day,1,@EndTime)
end
else if(@ID='2')
begin
select * from tb_shiftCheck where f_startTime between @StartTime and dateadd(day,1,@EndTime)
end
else
begin
select * from tb_weeklyM where f_startTime between @StartTime and dateadd(day,1,@EndTime)
end
改成这样:
Create PROCEDURE dbo.sp_getStatisticInfo( @ID nvarchar(50) )
AS
Case @ID
when '1' then
begin
select * from tb_dailyCheck where f_startTime between @StartTime and dateadd(day,1,@EndTime)
end
when '2' then
begin
select * from tb_shiftCheck where f_startTime between @StartTime and dateadd(day,1,@EndTime)
end
else
begin
select * from tb_weeklyM where f_startTime between @StartTime and dateadd(day,1,@EndTime)
end
前一段是可以的,但是后一段运行的时候总是语法错误,望大虾指点啊:-)
比如下面这段,也是跑不起来的:
Create procedure Test1(@Name varchar(50))
As
Case When @Name='s' Then (print 'DName')
When @Name='d' Then (print 'did')
Else (print 'dids')
End
去掉小括号同样跑不起来
望大虾指点一二。。。 展开
3个回答
展开全部
不是太懂sqlserver的procedures,但是看你用case when写的这一段,貌似最后缺少一个end吧
case when的最后应该有esle ... end 吧。
case when的最后应该有esle ... end 吧。
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
case 字段when 值 then else 值end
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询