1. 程式人生 > >DB2動態遊標語法例項

DB2動態遊標語法例項

CREATE PROCEDURE testDynamicCursor(IN flag CHAR(1))
LANGUAGE SQL
BEGIN
   DECLARE v_sql VARCHAR(1000);
   DECLARE v_st STATEMENT;
   DECLARE v_cursor CURSOR WITH RETURN FOR v_st;
  
   IF (flag = '1') THEN
    SET v_sql = 'SELECT * FROM TEST1';
  ELSE
    SET v_sql = 'SELECT * FROM TEST2';
  END IF;
  
   PREPARE v_st FROM v_sql;
   OPEN v_cursor;
END