By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,758 Members | 1,250 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,758 IT Pros & Developers. It's quick & easy.

Variable table name for cursor

P: 37
Hello all

What is the appropriate syntax for declaring a cursor for a table name that is passed as a parameter to a procedure? I tried several combination and it doesn't seem to work.

CREATE PROCEDURE PROC(tableName varchar(50))
BEGIN
DECLARE cursorName CURSOR FOR SELECT column1,column2 FROM tableName;
END

The select statement can not be a variable, also it's not working with PREPARE statement.

Can anyone help me on this matter?
Thanks in advance :)
May 5 '09 #1
Share this Question
Share on Google+
1 Reply


blyxx86
100+
P: 256
Does this help you?

Expand|Select|Wrap|Line Numbers
  1. create procedure cursorproc(IN p_in INT, OUT p_out VARCHAR(30))
  2. begin
  3.  
  4.    declare l_emp_name VARCHAR(30);
  5.    declare cur_1 cursor for select emp_name from emps where emp_id = p_in;
  6.  
  7.    open cur_1;
  8.    fetch cur_1 into l_emp_name;
  9.    close cur_1;
  10.  
  11.    set p_out = l_emp_name;
  12.  
  13. end;
  14.  
May 6 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.