Connecting Tech Pros Worldwide Help | Site Map

Variable table name for cursor

Member
 
Join Date: Aug 2007
Posts: 35
#1: May 5 '09
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 :)
blyxx86's Avatar
Familiar Sight
 
Join Date: Nov 2006
Posts: 255
#2: May 7 '09

re: Variable table name for cursor


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.  
Reply

Tags
cursor, variable table name