SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Declared variables error on second query?
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

sportsguy
Starting Member

USA
38 Posts

Posted - 03/05/2013 :  14:17:40  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

Germany
8765 Posts

Posted - 03/05/2013 :  14:23:31  Show Profile  Visit webfred's Homepage  Reply with Quote
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

USA
38 Posts

Posted - 03/05/2013 :  16:35:22  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000