Search code examples
oracle-databasesqlplusoracle19c

Oracle db pass column as object to function


Is it possible to pass the column of the current row as an object like variable so i can extract some informations from that in a function? The function can be inside a package

For example

Select myfunction(column1) from dual;

And then in the function get the the information of this column like an object

Function myfunction(objCol object)
Return something
Is
... declaration 
Begin
  schema := objCol.schema
  table := objCol.table_name
 ... 
  do some other things
  return something 
End

Something like that?


Solution

  • Oracle is not object-oriented by default and it will not pass a normal column as an object enhanced with metadata about itself. Without creating custom object types and variables and/or tables, columns and nested tables that use those custom object types, all the values you pass will be scalars. So, with a normal table you'd have to pass in not only the column value, but any attributes about that column you need (schema, table name, column name, etc.):

    CREATE OR REPLACE FUNCTION myfunction (owner IN varchar2, table_name IN varchar2, column_name IN varchar2, column_value IN varchar2)
      RETURN something
    AS
    
    BEGIN
      -- do something with owner, table_Name, column_name and column_value
      RETURN something
    END;
    

    /

    SELECT myfunction('MYSCHEMA','MYTABLE','MYCOLUMN',mycolumn)
      FROM myschema.mytable
    

    Also, Oracle is hard-typed, so if you want a generic function you'll need to create overloads (multiple definitions in a package with the same function name) for different possible datatypes of my_column/column_value. One for strings, one for numbers, one for dates, etc..