Den "fuldstændige" dynamiske version ville være denne (ikke testet):
DECLARE
v_stmt_str VARCHAR2(200);
v_cur INTEGER;
v_rows_processed INTEGER;
col_cnt INTEGER;
rec_tab DBMS_SQL.DESC_TAB;
rec DBMS_SQL.DESC_REC;
num_var NUMBER;
string_var VARCHAR2(4000);
date_var DATE;
-- .. some more variables if needed
BEGIN
v_cur := DBMS_SQL.OPEN_CURSOR; -- open cursor
v_stmt_str := 'SELECT whatever from ....';
DBMS_SQL.PARSE(v_cur, v_stmt_str, DBMS_SQL.NATIVE);
DBMS_SQL.DESCRIBE_COLUMNS(v_cur, col_cnt, rec_tab);
FOR c in 1..col_cnt LOOP
rec := rec_tab(c);
IF rec.col_type = DBMS_TYPES.TYPECODE_NUMBER THEN
DBMS_SQL.DEFINE_COLUMN(v_cur, c, num_var);
ELSIF rec.col_type = DBMS_TYPES.TYPECODE_VARCHAR2 THEN
DBMS_SQL.DEFINE_COLUMN(v_cur, c, string_var, rec.col_max_len);
ELSIF rec.col_type = DBMS_TYPES.TYPECODE_DATE THEN
DBMS_SQL.DEFINE_COLUMN(v_cur, c, date_var);
-- .. some more data types if needed
END IF;
END LOOP;
-- Execute
v_rows_processed := DBMS_SQL.EXECUTE(v_cur);
LOOP
-- Fetch a row
IF DBMS_SQL.FETCH_ROWS(v_cur) > 0 THEN
FOR c in 1..col_cnt LOOP
rec := rec_tab(c);
IF rec.col_type = DBMS_TYPES.TYPECODE_NUMBER THEN
DBMS_SQL.COLUMN_VALUE(v_cur, c, num_var);
ELSIF rec.col_type = DBMS_TYPES.TYPECODE_VARCHAR2 THEN
DBMS_SQL.COLUMN_VALUE(v_cur, c, string_var);
ELSIF rec.col_type = DBMS_TYPES.TYPECODE_DATE THEN
DBMS_SQL.COLUMN_VALUE(v_cur, c, date_var);
-- .. some more data types if needed
END IF;
END LOOP;
-- Process: do something with num_var or string_var or date_var values
ELSE
EXIT;
END IF;
END LOOP;
DBMS_SQL.CLOSE_CURSOR(v_cur); -- close cursor
END;
/
Men som allerede nævnt i kommentarer, tjek dine krav, hvis du virkelig har brug for det så dynamisk - sandsynligvis ikke.
Tjek Oracle-dokumentationen Coding Dynamic SQL og DBMS_SQL for andre eksempler. Der er forskellige metoder, der giver forskellige "niveauer" af dynamik.