0

我有以下查询按原样工作(人们可能会畏缩,但尽量忽略它有多糟糕):

DECLARE @submit_day DATETIME;
DECLARE @meeting_day DATETIME;
DECLARE @start_time_of_business_day DATETIME;
DECLARE @business_day_hours FLOAT;
DECLARE @submit_time DATETIME;
DECLARE @meeting_time DATETIME;
DECLARE @num1 FLOAT
DECLARE @num2 FLOAT
DECLARE @num3 FLOAT

SET @meeting_day = '2013-06-24';  -- USER GENERATED VARIABLE
SET @meeting_time = '15:45'; -- USER GENERATED

SET @submit_day = CONVERT(VARCHAR(10),GETDATE(),101);
SET @submit_time =  CONVERT(VARCHAR(8),GETDATE(),108);

SET @start_time_of_business_day = '09:00';
SET @business_day_hours = 8.5;

SET @num1 = ((DATEDIFF(dd, @submit_day, @meeting_day))
-(DATEDIFF(wk, @submit_day, @meeting_day) * 2)
-(CASE WHEN DATEPART(dw, @submit_day) = 1 THEN 1 ELSE 0 END)
-(CASE WHEN DATEPART(dw, @meeting_day) = 7 THEN 1 ELSE 0 END)
-(SELECT COUNT(*) FROM intranet.dbo.bank_holiday WHERE the_date BETWEEN @submit_day AND @meeting_day)) * @business_day_hours
SET @num2 = (select datediff(minute, @start_time_of_business_day, @submit_time)) / 60.0
SET @num3 = (select datediff(minute, @start_time_of_business_day, @meeting_time)) / 60.0

select @num1 - @num2 + @num3 as [hours]

所以我想把它设置为一个存储过程,所以我尝试了以下方法:

USE [INTRANET]
GO
/****** Object:  StoredProcedure [dbo].[BusinessHours]    Script Date: 06/21/2013 15:19:47 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[BusinessHours]

@meeting_date DATETIME,
@meeting_time DATETIME

AS

DECLARE @submit_day DATETIME;
DECLARE @submit_time DATETIME;

DECLARE @start_time_of_business_day DATETIME;
DECLARE @business_day_hours FLOAT;

DECLARE @num1 FLOAT
DECLARE @num2 FLOAT
DECLARE @num3 FLOAT

SET @submit_day = CONVERT(VARCHAR(10),GETDATE(),101);
SET @submit_time =  CONVERT(VARCHAR(8),GETDATE(),108);

SET @start_time_of_business_day = '09:00';
SET @business_day_hours = 8.5;

SET @num1 = ((DATEDIFF(dd, @submit_day, @meeting_day))
-(DATEDIFF(wk, @submit_day, @meeting_day) * 2)
-(CASE WHEN DATEPART(dw, @submit_day) = 1 THEN 1 ELSE 0 END)
-(CASE WHEN DATEPART(dw, @meeting_day) = 7 THEN 1 ELSE 0 END)
-(SELECT COUNT(*) FROM intranet.dbo.bank_holiday WHERE the_date BETWEEN @submit_day AND @meeting_day)) * @business_day_hours
SET @num2 = (select datediff(minute, @start_time_of_business_day, @submit_time)) / 60.0
SET @num3 = (select datediff(minute, @start_time_of_business_day, @meeting_time)) / 60.0

select @num1 - @num2 + @num3 as [hours]

这给了我一个错误:

消息 137,级别 15,状态 2,过程 BusinessHours,第 25 行
必须声明标量变量“@meeting_day”。
消息 137,级别 15,状态 2,过程 BusinessHours,第 29 行
必须声明标量变量“@meeting_day”。

尝试搜索,但无法弄清楚如何使其工作。

4

2 回答 2

2

是的。您将DECLAREd 变量重命名@meeting_day为 parameter @meeting_date

修复它。

于 2013-06-21T14:55:26.383 回答
1

您在参数声明中键入了“meeting_date”而不是“meeting_day”。

CREATE PROCEDURE [dbo].[BusinessHours]

@meeting_day DATETIME, --ERROR IS HERE
@meeting_time DATETIME

AS
于 2013-06-21T14:53:40.213 回答