Click here to Skip to main content
12,076,925 members (47,180 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: SQL-Server SQL-Server-2008 , +
i have error massege in sql query.msg is

Msg 137, Level 15, State 2, Line 1
Must declare the scalar variable "@emp_name".
Msg 137, Level 15, State 2, Line 3
Must declare the scalar variable "@emp_name".
Msg 137, Level 15, State 2, Line 4
Must declare the scalar variable "@emp_add".
Msg 137, Level 15, State 2, Line 8
Must declare the scalar variable "@emp_name".

plzzz tell me how can i solve these error. my query is
---declare cursor
declare @emp_name varchar(40)
declare @emp_add varchar(40)
 
---define cursor

declare cursor_employee cursor for
select emp_name,emp_add from employee
 
---open a cursor
open cursor_employee
 
---fetch the cursor
fetch next from cursor_employee into @emp_name,@emp_add
 
print '*emp_name='+ @emp_name
print 'emp_add='+ @emp_add
while(@@fetch_status=0)
begin
---fetch cursor for next rows
fetch next from cursor_employee into @emp_name,@emp_add
end
Posted 5-Sep-12 21:37pm
Edited 5-Sep-12 22:08pm
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

Hii,
I think you forgot to select declaration part of this cursor, thats why error occured.
You must select All cursor and then excecute.it will excecute successfully.
Becoz i had done this it was run successfuly.

Good luck
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Hi Rashid,

Please check the @emp_name,emp_add .

I have try same query for another table.but i got answer

Please check.

<pre lang="sql">declare @taskid varchar(40)
declare @asssto varchar(40)
 
---define cursor

declare cursor_employee cursor for
select taskid,asssto from tasklist
 
---open a cursor
open cursor_employee
 
---fetch the cursor
fetch next from cursor_employee into @taskid,@asssto
 
print  @taskid
print  @asssto
while(@@fetch_status=0)
begin
---fetch cursor for next rows
fetch next from cursor_employee into @taskid,@asssto
end
close cursor_employee
 
deallocate cursor_employee
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

The code is perfectly working. I believe you have forgot to include these lines when executing or commented it.


declare @emp_name varchar(40)
declare @emp_add varchar(40)
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160212.1 | Last Updated 6 Sep 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100