In PL/SQL is it possible to use a variable as a query a table?
Tried:
declare
TYPE t_name IS TABLE OF varchar(50) INDEX BY PLS_INTEGER;
v_names t_name;
begin
select name bulk collect into v_names from my_table;
select name from v_names where name = 'Max';
end;
Yes ... but not how you are doing it, for two reasons:
INDEX BY PLS_INTEGER
so what you have is an associative array.So, first you need to create the type:
CREATE TYPE t_name IS TABLE OF VARCHAR2(50);
Then you can run the PL/SQL block:
DECLARE
v_names t_name;
v_name VARCHAR2(50);
BEGIN
SELECT name
BULK COLLECT INTO v_names
FROM my_table;
SELECT COLUMN_VALUE
INTO v_name
FROM TABLE(v_names)
WHERE COLUMN_VALUE = 'Max';
DBMS_OUTPUT.PUT_LINE( v_name );
END;
/
(Note: the table collection expression in the second query has the pseudo-column COLUMN_VALUE
rather than any particular identifier from a table.)
db<>fiddle here