In the following query select_string can return any number of columns and rows with any possible data types.
Execute immediate \'select_string\' into v_table_var
The "full blown" dynmamic version would be this one (not tested):
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;
/
But as already stated in comments, check your requirements if you really need it such dynamic - probably not.
Check Oracle documentation Coding Dynamic SQL and DBMS_SQL for other examples. There are various methods providing different "levels" of dynamic.