Avatar of GeniusSteals
GeniusSteals asked on

Problem with SET variable statement in Stored Proc

Hi again..today's problem comes from trying to assign a couple of variables based on the value of another variable. In this instance, the variable @RetailWeek is passed to the procedure which should then use this to work out the MinDate and MaxDate for that week referenced in a table.

I see lots of code about but not sure where I am meant to put it in respect of the ALTER PROCEDURE statement. When I try to execute the SP I get the error message:
Procedure or Function 'proc_Update_OP_Single_Week' expects parameter '@MinDate', which was not supplied.

I'll add that I have put the SET statements before AS, after AS (as they are now) and in the main --Insert statements section...
ALTER PROCEDURE [dbo].[proc_Update_OP_Single_Week] 
	-- Add the parameters for the stored procedure here
	(@RetailWeek varchar(6),
	@MinDate datetime,
	@MaxDate datetime)
AS
SET @MinDate = (SELECT MIN(Date) FROM tRetailWeeks WHERE AgrWeek = @RetailWeek)
SET @MaxDate = (SELECT MAX(Date) FROM tRetailWeeks WHERE AgrWeek = @RetailWeek)
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
    -- Insert statements for procedure here
SELECT  Right(sales_person,5) AS SalesPer5Char,branch_code,reg_no,tran_no,tran_date,SUM(sales_qty) AS Quantity,SUM(sales_value) AS Net_Amount, SUM(tax_value) AS Tax_Amount
FROM 
SQLIIS.rstar.dbo.saleanalysis
WHERE sales_value<>0 AND tran_date>@MinDate AND tran_Date<@MaxDate
GROUP BY Right(Sales_person,5), branch_code, reg_no, tran_no, tran_date
ORDER BY tran_date
END

Open in new window

Microsoft SQL Server 2005

Avatar of undefined
Last Comment
GeniusSteals

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Qlemo

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
SOLUTION
Guy Hengel [angelIII / a3]

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
GeniusSteals

Many thanks to you both, I have only just started programming with SQL Server coming from a VB/VBA background and didn't realise how similiar it's requirements were.  It now seems obvious that they should be declared as local variables, so a valuable lesson learnt.

Cheers!
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck