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
 Declared variables error on second query?

Author  Topic 

sportsguy
Starting Member

39 Posts

Posted - 2013-03-05 : 14:17:40
I have declared the veriable, set the variable, and the second query fails with the error message of
Must declare the scalar variable "@YYYYMM".


USE [DatabaseName]
GO

DECLARE @YYYYMM INT
SET @YYYYMM = 201305

/**** UPDATE TRAILING PROJECT STATUS ****/
UPDATE ptd
SET ptd.Status = 'T'
FROM dbo.CTC_PTD ptd
INNER JOIN dbo.CTC_FINISHED fin ON ptd.strProject = fin.strProject AND ptd.[System] = fin.[System]
WHERE ptd.FiscalPeriod = @YYYYMM AND fin.Finished_Period < @YYYYMM
GO

/**** UPDATE TIP INTO CTC_PTD ****/

UPDATE ctc
SET ctc.Current_TIP_Approved = tip.TIP_Amount, ctc.TIP = tip.TIP_Current, ctc.PTD_TIP = tip.TIP_Current + tip.TIP_Previous
FROM dbo.CTC_PTD ctc
INNER JOIN TIP_RECOGNITION tip ON ctc.strProject = tip.strProject AND ctc.[System] = tip.[System] AND ctc.FiscalPeriod = tip.FiscalPeriod
WHERE ctc.FiscalPeriod = @YYYYMM
GO


IS a variable only good once? Do i have to create a procedure or a function?
If I do create a procedure or function, where do I save it?

I have A book, but this detail is missing from the chapter. . .

thanks,
sportsguy

MS Access 20 years, SQL hack

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2013-03-05 : 14:23:31
The GO is the end of a batch and after that the vars are unknown.


Too old to Rock'n'Roll too young to die.
Go to Top of Page

sportsguy
Starting Member

39 Posts

Posted - 2013-03-05 : 16:35:22
webfred

THANKS! i didn't see that in the book, but i tried it and worked perfectly!

makes sense as well. . .



MS Access 20 years, SQL hack
Go to Top of Page
   

- Advertisement -