Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a function with Rowtype parameter from a select statement in Oracle

I have an oracle function which has an in parameter which is of rowtype of a table, from a select statement i need to pass the current row to this function so that it does some processing and returns a value. Is there a pseudo variable that can be used within the context of a select statement something equivalent to a old and new in trigger.

I would like to do something like

select *,function1(rowtype) from table1

I want to avoid passing multiple parameters, so the question should be seen in that context.

like image 418
Dinesh Manne Avatar asked Mar 03 '09 10:03

Dinesh Manne


People also ask

How do you use %Rowtype?

Each field assumes the data type of its corresponding column. The %ROWTYPE attribute is prefixed by a cursor name or a cursor variable name. The syntax is record cursor%ROWTYPE , where record is an identifier that is assigned to the record, and cursor is an explicitly declared cursor within the current scope.

What is the use of %type and Rowtype explain with example?

Example# %TYPE : Used to declare a field with the same type as that of a specified table's column. %ROWTYPE: Used to declare a record with the same types as found in the specified table, view or cursor (= multiple columns).

What does Rowtype mean in Oracle?

The %ROWTYPE attribute provides a record type that represents a row in a database table. The record can store an entire row of data selected from the table or fetched from a cursor or cursor variable. Fields in a record and corresponding columns in a row have the same names and datatypes.

What is %Rowtype and %type in Oracle?

%TYPE provides the data type of a variable or a database column to that variable. %ROWTYPE provides the record type that represents a entire row of a table or view or columns selected in the cursor.


1 Answers

You can't do this with %ROWTYPE. %ROWTYPE is actually a PL/SQL record type, which is not a legal type in SQL, so you can't use it in a SELECT. You should create an object type which has the same columns as the table, change to function to expect that object type instead of %ROWTYPE, and then you can write something like this:

SELECT function(table1_typ(column1, column2, column3))
  FROM table1 t1

Drawbacks: You still have to type all the columns in the SELECT, and if you change the table, you will need to change the object type and the SELECT too.

like image 119
Gabor Kecskemeti Avatar answered Sep 30 '22 03:09

Gabor Kecskemeti