Click here to Skip to main content
15,892,575 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
SQL
---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
Updated 5-Sep-12 21:08pm
v2

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
 
Share this answer
 
Hi Rashid,

Please check the @emp_name,emp_add .

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

Please check.

SQL
<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
 
Share this answer
 
The code is perfectly working. I believe you have forgot to include these lines when executing or commented it.


SQL
declare @emp_name varchar(40)
declare @emp_add varchar(40)
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900