问题
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.
回答1:
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.
回答2:
Why not just pass the id of the row in and make the function work on that?
EDIT: You might be able to construct an OBJECT / TYPE on the fly and pass that to a function. Then you'll only need to query for the data once. Let me know if you need an example.
回答3:
You can do this:
DECLARE
foo table1%ROWTYPE;
BEGIN
function1(foo);
END;
EDIT: You can check out this blog post for a more detailed description of how to use this technique.
来源:https://stackoverflow.com/questions/605925/how-to-call-a-function-with-rowtype-parameter-from-a-select-statement-in-oracle