Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Novice needing help

Author  Topic 

calladrum
Starting Member

6 Posts

Posted - 2011-09-06 : 08:20:11
Hi All

I'm using a SQL query set up by someone else which has been working well for a while but now is throwing up the following error:

Msg 137, Level 15, State 2, Line 17
Must declare the variable '@Deduction'.

The query code is:

DECLARE @YearWeekFrom AS INTEGER
DECLARE @YearWeekTo AS INTEGER

------------------------------------------------------------
SET @YearWeekFrom = 201105 -- Change this value!
SET @YearWeekTo = 201105 -- Change this value!
------------------------------------------------------------

SELECT Year, Week, CCode, SUM(ISNULL(NUM_Promos,0)) AS NumPromos,
SUM(ISNULL(Total_Value,0)) AS TotalValue, SUM(ISNULL(Commission_Tot,0)) AS TotalCommission
FROM Promos
WHERE Deduction_Code_Inverse_Key = @Deduction AND ((Year * 100) + Week) >= @YearWeekFrom AND ((Year * 100) + Week) <= @YearWeekTo
GROUP BY Year, Week, CCode
ORDER BY Year, Week, CCode

Any ideas about why this isn't working?

Thanks

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-09-06 : 08:33:13
quote:
Originally posted by calladrum

Hi All

I'm using a SQL query set up by someone else which has been working well for a while but now is throwing up the following error:

Msg 137, Level 15, State 2, Line 17
Must declare the variable '@Deduction'.

The query code is:

DECLARE @Deduction AS INTEGER

DECLARE @YearWeekFrom AS INTEGER
DECLARE @YearWeekTo AS INTEGER

------------------------------------------------------------
SET @YearWeekFrom = 201105 -- Change this value!
SET @YearWeekTo = 201105 -- Change this value!
------------------------------------------------------------

SELECT Year, Week, CCode, SUM(ISNULL(NUM_Promos,0)) AS NumPromos,
SUM(ISNULL(Total_Value,0)) AS TotalValue, SUM(ISNULL(Commission_Tot,0)) AS TotalCommission
FROM Promos
WHERE Deduction_Code_Inverse_Key = @Deduction AND ((Year * 100) + Week) >= @YearWeekFrom AND ((Year * 100) + Week) <= @YearWeekTo
GROUP BY Year, Week, CCode
ORDER BY Year, Week, CCode

Any ideas about why this isn't working?

Thanks



missed declaring it
i hope its type is integer else change accordingly

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

calladrum
Starting Member

6 Posts

Posted - 2011-09-06 : 08:52:06
Thanks, I'll give this a try!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-09-06 : 11:07:10
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

calladrum
Starting Member

6 Posts

Posted - 2011-09-07 : 11:39:07
Hello again

I added in DECLARE @Deduction AS VARCHAR (32)

and now the query appears to run but no results come in (and I know there should be) and there are no error messages. Have checked that all fields do appear in the PROMOS SQL Table. Any suggestions?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-09-09 : 02:43:32
quote:
Originally posted by calladrum

Hello again

I added in DECLARE @Deduction AS VARCHAR (32)

and now the query appears to run but no results come in (and I know there should be) and there are no error messages. Have checked that all fields do appear in the PROMOS SQL Table. Any suggestions?


ok. what was the value you passed for @Deduction? without passing a value it assumes NULL value and you wont get any result

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -