简体   繁体   English

如何使用Oracle中的select语句调用带有Rowtype参数的函数

[英]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. 我有一个oracle函数,它有一个in参数,它是一个表的rowtype,从select语句我需要将当前行传递给这个函数,以便它进行一些处理并返回一个值。 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. 是否有一个伪变量可以在select语句的上下文中使用,等同于触发器中的旧的和新的。

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. 我想避免传递多个参数,因此应该在该上下文中看到问题。

You can't do this with %ROWTYPE. 您无法使用%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. %ROWTYPE实际上是PL / SQL记录类型,它在SQL中不是合法类型,因此您不能在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: 您应该创建一个与表具有相同列的对象类型,更改为函数以期望该对象类型而不是%ROWTYPE,然后您可以编写如下内容:

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. 缺点:您仍然需要在SELECT中键入所有列,如果更改表,则还需要更改对象类型和SELECT。

Why not just pass the id of the row in and make the function work on that? 为什么不直接传入行的id并使函数工作呢?


EDIT: You might be able to construct an OBJECT / TYPE on the fly and pass that to a function. 编辑:您可以动态构建OBJECT / TYPE并将其传递给函数。 Then you'll only need to query for the data once. 然后,您只需要查询一次数据。 Let me know if you need an example. 如果您需要一个例子,请告诉我。


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. 编辑:您可以查看此博客文章 ,了解如何使用此技术的更详细说明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM