返回从选择查询检索到的数据的函数 - Oracle

发布于 2024-10-13 04:57:36 字数 311 浏览 4 评论 0原文

我正在尝试编写一个返回选择查询结果的函数。我使用过非常基本的函数,它们会返回一个数字和一个 varchar2(string)。但现在我想返回一个选择的结果,比如 10 行及其相应的列。

我将如何编写该函数以及返回类型是什么?

我编写的一个示例函数是:

创建或替换函数 func1 返回 varchar2 作为开始 return('来自 func1 的你好');结束函数1;

我仍处于基础水平,所以有人可以帮助我返回选择查询的结果吗?我相信应该使用游标,因为会有不止一行。

I am trying to write a function that would return the result of a select query. I have worked with very basic functions that would return a number and a varchar2(string). But now I want to return the result of a select, which would be like 10 rows and their corresponding columns.

How would I write the function and what would the return type be?

An example function that I have written is:

create or replace function func1
return varchar2 as begin
return('hello from func1'); end func1;

I am still at a basic level, so can anybody help me out with returning the result of a select query? I believe cursors are to be used, as there would be more than one row.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

把时间冻结 2024-10-20 04:57:36

通常,函数返回单个“事物”。通常,这是一个标量(数字、varchar2、记录等),但您可以返回集合。因此,例如,您可以返回一个集合(在本例中为嵌套表),其中包含 EMP 表中的所有 EMPNO 值,

CREATE TYPE empno_tbl 
    IS TABLE OF NUMBER;

CREATE OR REPLACE FUNCTION get_empnos
  RETURN empno_tbl
IS
  l_empnos empno_tbl;
BEGIN
  SELECT empno
    BULK COLLECT INTO l_empnos
    FROM emp;
  RETURN l_empnos;
END;

但这在函数中并不是特别常见的事情。更常见的是让函数返回游标而不是返回值,并让调用者处理数据的获取,即,

CREATE OR REPLACE FUNCTION get_empnos2
  RETURN SYS_REFCURSOR
IS
  l_rc SYS_REFCURSOR;
BEGIN
  OPEN l_rc
   FOR SELECT empno
         FROM emp;
  RETURN l_rc;
END;

但即使这样在 Oracle 中也不是特别常见。根据您想要完成的任务,通常更常见的做法是简单地创建一个选择您感兴趣的数据的视图并查询该视图,而不是调用函数或过程。

Normally, a function returns a single "thing". Normally, that is a scalar (a number, a varchar2, a record, etc) though you can return a collection. So, for example, you could return a collection (in this case a nested table) with all the EMPNO values from the EMP table

CREATE TYPE empno_tbl 
    IS TABLE OF NUMBER;

CREATE OR REPLACE FUNCTION get_empnos
  RETURN empno_tbl
IS
  l_empnos empno_tbl;
BEGIN
  SELECT empno
    BULK COLLECT INTO l_empnos
    FROM emp;
  RETURN l_empnos;
END;

But this isn't a particularly common thing to do in a function. It would be a bit more common to have the function return a cursor rather than returning values and to let the caller handle fetching the data, i.e.

CREATE OR REPLACE FUNCTION get_empnos2
  RETURN SYS_REFCURSOR
IS
  l_rc SYS_REFCURSOR;
BEGIN
  OPEN l_rc
   FOR SELECT empno
         FROM emp;
  RETURN l_rc;
END;

But even that isn't particularly common in Oracle. Depending on what you're trying to accomplish, it would generally be more common to simply create a view that selected the data you were interested in and to query that view rather than calling a function or procedure.

昇り龍 2024-10-20 04:57:36

好吧,如果你刚刚学习,你应该了解管道函数。管道函数允许您返回 PLSQL 中动态生成的表。

例如...

  create function
      gen_numbers(n in number default null)
      return array
      PIPELINED
  as
  begin
     for i in 1 .. nvl(n,999999999)
         loop
         pipe row(i);
     end loop;
    return;
  end;

我从 http://www.akadia.com/services/ora_pipe_functions 借用的。 html:-)

Well, if you're just learning, you should know about pipelined functions. A pipelined function lets you return dynamically generated tables within PLSQL.

For example...

  create function
      gen_numbers(n in number default null)
      return array
      PIPELINED
  as
  begin
     for i in 1 .. nvl(n,999999999)
         loop
         pipe row(i);
     end loop;
    return;
  end;

Which I borrowed from http://www.akadia.com/services/ora_pipe_functions.html :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文