加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 数据库 > Oracle > 正文

在Oracle的函数中,返回表类型的语句

发布时间:2020-05-29 08:33:49 所属栏目:Oracle 来源:互联网
导读:在SQL Server中有表变量,可以在function中方便地返回,习惯SQL Server或者需要把脚本从SQL Server转到Oracle中的朋友可以都会碰到这个问题

Oracle的function中怎么返回表变量? 太晚了,过多的理论知识就不说了,下面简单地说实现吧!..

1、创建表对象类型。

在Oracle中想要返回表对象,必须自定义一个表类型,如下所示:
<div class="codetitle"><a style="CURSOR: pointer" data="31371" class="copybut" id="copybut31371" onclick="doCopy('code31371')"> 代码如下:<div class="codebody" id="code31371">
create or replace type t_table is table of number;

上面的类型定义好后,在function使用可用返回一列的表,如果需要多列的话,需要先定义一个对象类型。然后把对象类型替换上面语句中的number; 定义对象类型:
<div class="codetitle"><a style="CURSOR: pointer" data="23085" class="copybut" id="copybut23085" onclick="doCopy('code23085')"> 代码如下:<div class="codebody" id="code23085">
create or replace type obj_table as object
(
id int,
name varchar2(50)
)

修改表对象类型的定义语句如下:
<div class="codetitle"><a style="CURSOR: pointer" data="1059" class="copybut" id="copybut1059" onclick="doCopy('code1059')"> 代码如下:<div class="codebody" id="code1059">
create or replace type t_table is table of obj_table;

2、 创建演示函数


在函数的定义中,可以使用管道化表函数和普通的方式,下面提供两种使用方式的代码: 1)、管道化表函数方式:
<div class="codetitle"><a style="CURSOR: pointer" data="40580" class="copybut" id="copybut40580" onclick="doCopy('code40580')"> 代码如下:<div class="codebody" id="code40580">
create or replace function f_pipe(s number)
return t_table pipelined
as
v_obj_table obj_table;
begin
for i in 1..s loop
v_obj_table := obj_table(i,to_char(ii));
pipe row(v_obj_table);
end loop;
return;
end f_pipe;

注意:管道的方式必须使用空的return表示结束. 调用函数的方式如下:
<div class="codetitle"><a style="CURSOR: pointer" data="22017" class="copybut" id="copybut22017" onclick="doCopy('code22017')"> 代码如下:<div class="codebody" id="code22017">
select
from table(f_pipe(5));
2)、 普通的方式:
<div class="codetitle"><a style="CURSOR: pointer" data="43285" class="copybut" id="copybut43285" onclick="doCopy('code43285')"> 代码如下:<div class="codebody" id="code43285">
create or replace function f_normal(s number)
return t_table
as
rs t_table:= t_table();
begin
for i in 1..s loop
rs.extend;
rs(rs.count) := obj_table(rs.count,'name'||to_char(rs.count));
--rs(rs.count).name := rs(rs.count).name || 'xxxx';
end loop;
return rs;
end f_normal;

初始化值后还可以想注视行那样进行修改. 调用方式如下:
<div class="codetitle"><a style="CURSOR: pointer" data="69988" class="copybut" id="copybut69988" onclick="doCopy('code69988')"> 代码如下:<div class="codebody" id="code69988">
select * from table(f_normal(5));

ok 完成

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读