2013-06-21 95 views
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] 

這給了我一個錯誤:

Msg 137, Level 15, State 2, Procedure BusinessHours, Line 25
Must declare the scalar variable "@meeting_day".
Msg 137, Level 15, State 2, Procedure BusinessHours, Line 29
Must declare the scalar variable "@meeting_day".

試圖尋找,但無法弄清楚如何讓這個工作。

+0

你在哪裏宣佈「@meeting_day」? – Milen

回答

1

您在參數聲明中鍵入「meeting_date」而不是「meeting_day」。

CREATE PROCEDURE [dbo].[BusinessHours] 

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

AS 
2

是的。您將DECLARE d變量@meeting_day更名爲參數@meeting_date

修復此問題。