I have a query that returns multiple records based on the Number ID (parameter) passed into it. I would like to create a table function which takes in "num_id" (number datatype) as a parameter, passes that into a query, performs operations on every record of that query's result, and returns "mainKey" (character datatype) as a record.
Every tutorial I find has me create an Object and Table of that Object before I create the function.. I don't quite understand the use of this.
I also don't understand where I would add my original query (the one that retrieves records from a DB).
Can anyone please show me how I would achieve this? I was able to perform actions on every record
Here is my work so far:
-- create object
CREATE TYPE test_t AS OBJECT
(mainKey char);
-- create collection type:
CREATE TYPE testSet_t AS TABLE OF test_t;
--Create table function: "test_tf"
CREATE OR REPLACE FUNCTION test_tf
(num_id IN NUMBER)
RETURN char
IS
z char;
BEGIN
SELECT one_column_name from testSet_t
END;
I used to be able to perform operations in every record with a cursor and a for loop... but here I was only able to print data as dbms_output. I need to be able to produce some records as a result. Here is that cursor attempt:
DECLARE
cursor cur_test IS
<my select statement which returns 4 records>;
BEGIN
FOR ln_index IN cur_test
LOOP
DBMS_OUTPUT.put_line(ln_index.one_column_name);
DBMS_OUTPUT.put_line(ln_index.another_column_name);
END LOOP;
END;
The above code works fine and prints to dbms output... but I need to be able to perform operations to the data in each line, and return records instead of DBMS Output.
For Oracle 19c (19.7) and above you may use
SQL_MACROto create a table-valued function that may be used as parameterized view.fiddle
UPD. If you want to have a more generic way to apply something to arbitrary table, you may also use Polymorphic Table Function (PTF) concept, implementing transformation logic inside PTF package and applying it to the different tables (or CTEs). It is available since 18c.
Below is an example of application of the same
f_do_smthto the table and column specified in the function call (runtime), not in the implementation. Please note, thattabandcolsparameters (of typetableandcolumnsrespectively) are identifiers, so they cannot be parameterized (via bind variables or string expressions) in this example.And application of this function to different columns of the table.
valid