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
 printing error

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 )
go

print @sal char

when i try to print @sal it gives error
but the first code execute well, how can print it...
error:
Msg 137, Level 15, State 2, Line 1
Must 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]

Go to Top of Page

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
Go to Top of Page

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.

Go to Top of Page

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 MVP
http://visakhm.blogspot.com/

Go to Top of Page

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 error


declare @sal char(20)

select @sal = empname
from employee
where empid = 1

print @sal

Msg 137, Level 15, State 2, Line 1
Must 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
Go to Top of Page

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
Go to Top of Page

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 error

declare @sal char(20)

select @sal = empname
from employee
where empid = 1
go
print @sal
Go to Top of Page

kesharj
Starting Member

7 Posts

Posted - 2010-03-16 : 02:48:21
same problem in another code as well

declare @test money
set @test= (select max(salary) from employee )
go
select @test

error:
Msg 137, Level 15, State 2, Line 1
Must declare the scalar variable "@test".
Go to Top of Page

jet1337
Starting Member

11 Posts

Posted - 2010-03-16 : 03:04:05
working fine for me

ASPnix.com
Go to Top of Page

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 well

declare @test money
set @test= (select max(salary) from employee )
go
select @test

error:
Msg 137, Level 15, State 2, Line 1
Must declare the scalar variable "@test".


I already suggested you to remove go

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-16 : 04:09:53
quote:
Originally posted by jet1337

working fine for me

ASPnix.com


It wont if you copy pasted OPs code and try

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

kesharj
Starting Member

7 Posts

Posted - 2010-03-16 : 14:00:19
dear friends thank you for your kind helps
but
still i got the same problem

i just executed other code,

declare @tablename varchar(20)

select @tablename=tablename from dynsqlexamp where tableid=2

it executed successfully but when i tried to print it, it show the same error

print @tablename

error:
Msg 137, Level 15, State 2, Line 1
Must 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?

Go to Top of Page

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 MVP
http://visakhm.blogspot.com/

Go to Top of Page

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 execution

declare @tablename varchar(20)

select @tablename=tablename from dynsqlexamp where tableid=2

it executed successfully but when i tried to print it, it show the same error

print @tablename



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

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
Go to Top of Page
   

- Advertisement -