Variable table name for cursor

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dreea
    New Member
    • Aug 2007
    • 37

    Variable table name for cursor

    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
    Contributor
    • Nov 2006
    • 258

    #2
    Does this help you?

    Code:
    create procedure cursorproc(IN p_in INT, OUT p_out VARCHAR(30))
    begin
    
       declare l_emp_name VARCHAR(30);
       declare cur_1 cursor for select emp_name from emps where emp_id = p_in;
    
       open cur_1;
       fetch cur_1 into l_emp_name;
       close cur_1;
    
       set p_out = l_emp_name;
    
    end;

    Comment

    Working...