Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

must declare the scalar variable! Expand / Collapse
Author
Message
Posted Thursday, May 09, 2013 1:45 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, May 10, 2013 6:53 AM
Points: 4, Visits: 11
hello every body.
I have a question about declaring variable in sql server 2012.
when i declare a variable for example :
  DECLARE @X INT;  

when i compile this line it's true
but when i set a value in different line to this variable:
  SET  @X =12;  

it returns an error message as:
 Must declare the scalar variable @X 

how can i fix it?
Post #1450919
Posted Thursday, May 09, 2013 1:59 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 9:33 AM
Points: 6,754, Visits: 12,854
Can you post all of the code between the DECLARE and the SET?

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #1450922
Posted Thursday, May 09, 2013 2:03 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, May 10, 2013 6:53 AM
Points: 4, Visits: 11
thank you ChrisM@Work.
but I have not entered any code between DECLARE and SET my code is:
DECLARE @X INT;
SET @X =12;

Post #1450924
Posted Thursday, May 09, 2013 2:05 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 9:33 AM
Points: 6,754, Visits: 12,854
edalatfeizi (5/9/2013)
thank you ChrisM@Work.
but I have not entered any code between DECLARE and SET my code is:
DECLARE @X INT;
SET @X =12;



Not even this?

DECLARE @X INT;
GO
SET @X =12;



“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #1450926
Posted Thursday, May 09, 2013 2:10 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, May 10, 2013 6:53 AM
Points: 4, Visits: 11
no it's just
DECLARE @X INT;
SET @X =12;

Post #1450928
Posted Thursday, May 09, 2013 3:05 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 9:33 AM
Points: 6,754, Visits: 12,854
edalatfeizi (5/9/2013)
no it's just
DECLARE @X INT;
SET @X =12;



What are you using as your client?


“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #1450945
Posted Thursday, May 09, 2013 3:21 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 8:58 AM
Points: 5,077, Visits: 8,918
You need to run the two statements as one batch, since a variable will only "live" until the end of the batch.

John
Post #1450956
Posted Thursday, May 09, 2013 3:48 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 12:33 PM
Points: 14,800, Visits: 27,274
You sure there's not something else at work here? I just tested your code and it's working fine. There's no intervening code of any kind?

----------------------------------------------------
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood..." Theodore Roosevelt
The Scary DBA
Author of: SQL Server 2012 Query Performance Tuning
SQL Server 2008 Query Performance Tuning Distilled
and
SQL Server Execution Plans

Product Evangelist for Red Gate Software
Post #1450965
Posted Thursday, May 09, 2013 6:59 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Thursday, October 31, 2013 3:44 AM
Points: 314, Visits: 4,128
Your code run as a batch should work.

I think what you are probably doing is highlighting and running the statements individually which would reproduce the error you are getting.
Post #1451094
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse