返回从选择查询检索到的数据的函数 - Oracle
我正在尝试编写一个返回选择查询结果的函数。我使用过非常基本的函数,它们会返回一个数字和一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通常,函数返回单个“事物”。通常,这是一个标量(数字、varchar2、记录等),但您可以返回集合。因此,例如,您可以返回一个集合(在本例中为嵌套表),其中包含 EMP 表中的所有 EMPNO 值,
但这在函数中并不是特别常见的事情。更常见的是让函数返回游标而不是返回值,并让调用者处理数据的获取,即,
但即使这样在 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
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.
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.
好吧,如果你刚刚学习,你应该了解管道函数。管道函数允许您返回 PLSQL 中动态生成的表。
例如...
我从 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...
Which I borrowed from http://www.akadia.com/services/ora_pipe_functions.html :-)