oshirowanen
oshirowanen

Reputation: 15925

Converting query to a stored procedure

I have the following query which works as is (people will probably cringe, but try to ignore how bad it is):

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]

So I want to set this up as a stored procedure, so I tried the following:

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]

This gives me an error:

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".

Tried searching, but can't figure out how to get this to work.

Upvotes: 0

Views: 3404

Answers (2)

RBarryYoung
RBarryYoung

Reputation: 56735

Yes. You renamed your DECLAREd variable @meeting_day to the parameter @meeting_date.

Fix that.

Upvotes: 2

EkoostikMartin
EkoostikMartin

Reputation: 6911

You typed "meeting_date" instead of "meeting_day" in your parameter declaration.

CREATE PROCEDURE [dbo].[BusinessHours]

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

AS

Upvotes: 1

Related Questions