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

前端之家收集整理的这篇文章主要介绍了在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 完成

原文链接:https://www.f2er.com/oracle/65711.html
表类型

猜你在找的Oracle相关文章