我有以下Select语句,我需要创建一个存储过程,它将选择要运行的语句,猜测这必须基于变量(newb here)。我已经阅读了有关通过参数的内容,但在我的搜索中没有看到适用于这种情况的示例,请帮帮我。
我在SQL Server 2014中。
第一句话
select
cast(dateadd(hour,-4,getdate())-max(esig_date) as time) as time_since_last_esig,
case
when datepart(hour,dateadd(hour,-4,getdate()))>=8 and datepart(hour,dateadd(hour,-4,getdate()))<22 and cast(dateadd(hour,-4,getdate())-max(esig_date) as time)>='00:30:00.0000000' then 'alert'
else 'no alert'
end as alert_status
from activity_table
第二声明
select
cast(dateadd(hour,-4,getdate())-max(esig_date) as time) as time_since_last_esig,
case
when datepart(hour,dateadd(hour,-4,getdate()))<8 and datepart(hour,dateadd(hour,-4,getdate()))>=22 and cast(dateadd(hour,-4,getdate())-max(esig_date) as time)>='01:00:00.0000000' then 'alert'
else 'no alert'
end as alert_status
from activity_table
答案 0 :(得分:1)
像这样的工作
CREATE PROCEDURE ChooseWhichOne
@Selection BIT
AS
BEGIN
IF @Selection = 0
BEGIN
select
cast(dateadd(hour,-4,getdate())-max(esig_date) as time) as time_since_last_esig,
case
when datepart(hour,dateadd(hour,-4,getdate()))>=8 and datepart(hour,dateadd(hour,-4,getdate()))<22 and cast(dateadd(hour,-4,getdate())-max(esig_date) as time)>='00:30:00.0000000' then 'alert'
else 'no alert'
end as alert_status
from activity_table
END
ELSE
BEGIN
select
cast(dateadd(hour,-4,getdate())-max(esig_date) as time) as time_since_last_esig,
case
when datepart(hour,dateadd(hour,-4,getdate()))<8 and datepart(hour,dateadd(hour,-4,getdate()))>=22 and cast(dateadd(hour,-4,getdate())-max(esig_date) as time)>='01:00:00.0000000' then 'alert'
else 'no alert'
end as alert_status
from activity_table
END
RETURN 0
END
AND执行存储的PROCEDURE
EXEC dbo.ChooseWhichOne @Selection = 0 -- bit
EXEC dbo.ChooseWhichOne @Selection = 1 -- bit