| Author |
Topic |
|
kesharj
Starting Member
7 Posts |
Posted - 2010-03-15 : 21:33:08
|
| friends i have a problem:declare @sal char(20)set @sal= (select empname from employee where empid =1 )goprint @sal charwhen i try to print @sal it gives errorbut the first code execute well, how can print it... error:Msg 137, Level 15, State 2, Line 1Must declare the scalar variable "@sal". |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2010-03-15 : 22:15:00
|
[code]declare @sal char(20)select @sal = empname from employee where empid = 1 print @sal [/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
kesharj
Starting Member
7 Posts |
Posted - 2010-03-15 : 22:20:44
|
| thank you, kh for your help but when i print it with your code it gave the same error |
 |
|
|
haroon2k9
Constraint Violating Yak Guru
328 Posts |
Posted - 2010-03-16 : 00:12:09
|
quote: Originally posted by kesharj thank you, kh for your help but when i print it with your code it gave the same error
post your fullcode which gives this error. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-16 : 00:21:39
|
quote: Originally posted by kesharj thank you, kh for your help but when i print it with your code it gave the same error
It wont provided you removed the intermediate go as suggested by Khtan------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
kesharj
Starting Member
7 Posts |
Posted - 2010-03-16 : 02:11:54
|
| thanks friends n mr. kh i executed the same following code again but it gave the same errordeclare @sal char(20)select @sal = empname from employee where empid = 1 print @sal Msg 137, Level 15, State 2, Line 1Must declare the scalar variable "@sal".my operating system is windows 7, is that the problem of os as well i tried the code in another comp as well but the same error accord which have vista os |
 |
|
|
vaibhavktiwari83
Aged Yak Warrior
843 Posts |
Posted - 2010-03-16 : 02:20:25
|
| Perhaps you are writing full code but executing only last statement by selecting that only.Just copy and paste the code and press F5.Vaibhav T |
 |
|
|
kesharj
Starting Member
7 Posts |
Posted - 2010-03-16 : 02:29:43
|
| i executed all the code at same time and separately as well but same errordeclare @sal char(20)select @sal = empname from employee where empid = 1 goprint @sal |
 |
|
|
kesharj
Starting Member
7 Posts |
Posted - 2010-03-16 : 02:48:21
|
| same problem in another code as welldeclare @test moneyset @test= (select max(salary) from employee )goselect @testerror:Msg 137, Level 15, State 2, Line 1Must declare the scalar variable "@test". |
 |
|
|
jet1337
Starting Member
11 Posts |
Posted - 2010-03-16 : 03:04:05
|
| working fine for meASPnix.com |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-16 : 04:08:31
|
quote: Originally posted by kesharj same problem in another code as welldeclare @test moneyset @test= (select max(salary) from employee )goselect @testerror:Msg 137, Level 15, State 2, Line 1Must declare the scalar variable "@test".
I already suggested you to remove go------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-16 : 04:09:53
|
quote: Originally posted by jet1337 working fine for meASPnix.com
It wont if you copy pasted OPs code and try------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
kesharj
Starting Member
7 Posts |
Posted - 2010-03-16 : 14:00:19
|
| dear friends thank you for your kind helpsbutstill i got the same problemi just executed other code, declare @tablename varchar(20) select @tablename=tablename from dynsqlexamp where tableid=2it executed successfully but when i tried to print it, it show the same error print @tablenameerror:Msg 137, Level 15, State 2, Line 1Must declare the scalar variable "@tablename".i am sure i gives the error when i declare the value and print other ways all other code run perfectly.some frens said that i have to install windows 2003 server enterprise edition then it will work, i have windows 2007 os, i am confused what to do?i think i have to try with changing the os. isn't it? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-16 : 14:03:29
|
| i'm sure you're still having a go separating batches. plzz understand variables have only scope in current batch in which you declare them------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2010-03-16 : 14:05:28
|
how do you execute the code ?You just highlight the print @tablename and execute ? ? ? You have to execute the entire query in one single executiondeclare @tablename varchar(20)select @tablename=tablename from dynsqlexamp where tableid=2it executed successfully but when i tried to print it, it show the same errorprint @tablename KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
kesharj
Starting Member
7 Posts |
Posted - 2010-03-16 : 15:00:28
|
| thanx! frens, i really appreciate your helps, yes!now it worked.soon i will come with other problems ok. lol |
 |
|
|
|