Here is a table FRUITS –
FILTER A B C Apple 0 3 6 Orange 1 4 7 Apricot 2 5 8
Is it possible to create a function that returns column A for apple (0), B for Orange(4) and C for Apricot(8)?
SELECT Fruit_Function('Apple') FROM dual; --RESULT 0. SELECT Fruit_Function('Orange') FROM dual; --RESULT 4. SELECT Fruit_Function('Apricot') FROM dual; --RESULT 8.
Right now I have a function like this
CREATE OR REPLACE FUNCTION Fruit_Function (v_id integer, v_filter varchar2) RETURN varchar2 as v_rp varchar2(500); BEGIN SELECT ASSIGNEE INTO v_rp FROM FRUITS a WHERE a.id = v_id AND a.filter = v_filter; RETURN v_rp; END Fruit_Function;
This function returns the column name, but not the actual value.
Advertisement
Answer
Create a view!
create view v_t as select t.*, (case when filter = 'Apple' then a when filter = 'Orange' then b when filter = 'Apricot' then C end) as result from t;
Then, use the view for the queries in your package.
You can also add the column as a computed column directly into the table:
alter table t add result number generated always as (case when filter = 'Apple' then a when filter = 'Orange' then b when filter = 'Apricot' then C end);
With this approach, the column looks like it is part of the table.