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

浪子不回头ぞ 提交于 2021-02-07 13:58:39

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!