May 9, 2013 at 1:45 am
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?
May 9, 2013 at 1:59 am
Can you post all of the code between the DECLARE and the SET?
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
May 9, 2013 at 2:03 am
thank you ChrisM@Work.
but I have not entered any code between DECLARE and SET my code is:
DECLARE @X INT;
SET @X =12;
May 9, 2013 at 2:05 am
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;
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
May 9, 2013 at 2:10 am
no it's just
DECLARE @X INT;
SET @X =12;
May 9, 2013 at 3:05 am
edalatfeizi (5/9/2013)
no it's just
DECLARE @X INT;
SET @X =12;
What are you using as your client?
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
May 9, 2013 at 3:21 am
You need to run the two statements as one batch, since a variable will only "live" until the end of the batch.
John
May 9, 2013 at 3:48 am
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
Author of:
SQL Server Execution Plans
SQL Server Query Performance Tuning
May 9, 2013 at 6:59 am
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.
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply