zoukankan      html  css  js  c++  java
  • oracle函数返回结果集

    一.用自定义类型实现

    1、创建表对象类型。

       在Oracle中想要返回表对象,必须自定义一个表类型,如下所示:

    Sql代码  收藏代码
    1. create or replace type type_table is table of number;   

    上面的类型定义好后,在function使用可用返回一列的表,稍后介绍返回多列的

    2、 创建函数

    在函数的定义中,可以使用管道化表函数和普通的方式,下面提供两种使用方式的代码:

    1)、管道化表函数方式:

    Sql代码  收藏代码
    1. create or replace function f_pipe  
    2. (s number)  
    3. return type_table pipelined  
    4. as    
    5. begin      
    6.     for i in 1..s loop   
    7.     pipe row(i);   
    8. end loop;  
    9. return;  
    10. end f_pipe;  

      

    注意:管道的方式必须使用空的return表示结束.

    调用函数的方式如下:

    2)、 普通的方式:

    Sql代码  收藏代码
    1. create or replace function f_normal  
    2. (s number)  
    3. return type_table  
    4. as  
    5.     rs type_table:= type_table();  
    6. begin  
    7.     for i in 1..s loop  
    8.         rs.extend;  
    9.         rs(rs.count) := i;  
    10.     end loop;  
    11. return rs;  
    12. end f_normal;  

    调用方式如下:

    Sql代码  收藏代码
    1. select * from table(f_normal(5));  

    如果需要多列的话,需要先定义一个对象类型。可以把对象类型替换上面语句中的number;

    定义对象类型:

    Sql代码  收藏代码
    1. create or replace type type_row as object  
    2. (  
    3.   id int,  
    4.   name varchar2(50)  
    5. )  

    修改表对象类型的定义语句如下:

    Sql代码  收藏代码
    1. create or replace type type_table is table of type_row;  

    1)、管道化表函数方式:

    Sql代码  收藏代码
    1. create or replace function f_pipe(s number)  
    2. return type_table pipelined  
    3. as  
    4.     v_type_row type_row;     
    5. begin      
    6. for i in 1..s loop   
    7.     v_type_row :=  type_row(i,to_char(i*i));  
    8.     pipe   row(v_type_row);     
    9. end loop;  
    10. return;  
    11. end f_pipe;  

     测试:select * from table(f_pipe(5));

    2)、 普通的方式:

    Sql代码  收藏代码
    1. create or replace function f_normal(s number)  
    2. return type_table  
    3. as  
    4.     rs type_table:= type_table();  
    5. begin  
    6.     for i in 1..s loop  
    7.         rs.extend;  
    8.         rs(rs.count) := type_row(rs.count,'name'||to_char(rs.count));  
    9.     --Result(Result.count) := type_row(NULL,NULL);  
    10.   
    11.         --rs(rs.count).name := rs(rs.count).name || 'xxxx';  
    12.     end loop;  
    13. return rs;  
    14. end f_normal;  

      测试:select * from table(f_normal(5));

    其他代码段

    把权限(和)值拆分成多条记录

    Sql代码  收藏代码
    1. create or replace type type_table_number is table of number;   
    2.     
    3. create or replace function f_right_table(  
    4.     rights number  
    5. )  
    6.     --自定义table类型 --pipelined 管道关键字  
    7.     return type_table_number pipelined     
    8. as     
    9. begin  
    10.     --调用方法:select column_value as right from table(f_right_table(power(2,15)*2-2));     
    11.     for i in 1..15 loop    
    12.       IF bitand(rights,power(2,i))=power(2,i) THEN  
    13.          pipe row(power(2,i)); --pipe row 特定写法,输出记录  
    14.       END IF;    
    15.     end loop;  
    16. return;  
    17. end f_right_table;  

    一种遍历数据的方法,

    只是演示myrow,myrow 就相当于一个临时变量

    Java代码  收藏代码
    1. for myrow in (  
    2.       select 2 as right from dual where bitand(rights,2)=2  
    3.       union  
    4.       select 4 as right from dual where bitand(rights,4)=4  
    5. ) loop  
    6.       rs.extend;  
    7.       rs(rs.count) := myrow.right;  
    8. end loop;  

    二.其他实现
     

    包里面用一个存储过程,返回游标,就可以了

    >包的定义
    1) 包头

    Sql代码  收藏代码
    1. create or replace package mypk    
    2. as    
    3. type t_cursor is ref cursor;    
    4. procedure proc(name varchar2,c out t_cursor,a number);    
    5. end;  

    2) 包体

    Sql代码  收藏代码
    1. create or replace package body mypk    
    2. as    
    3. procedure proc(name varchar2,c out t_cursor,a number)    
    4. as     
    5. begin    
    6. open c for select * from test where id=a and name=name;    
    7. end proc;    
    8. end;  

    这个方案的局限性太大,无法实现select * from function()的要求

    调用:

    Sql代码  收藏代码
    1. declare  
    2.     cur_out_arg mypk.t_cursor;  
    3.     rec_arg test%rowtype;  
    4. begin  
    5.     mypk.proc('abc',cur_out_arg,3);  
    6.   --open cur_out_arg;           
    7.     loop             
    8.       --提取一行数据到rec_arg             
    9.       fetch cur_out_arg into rec_arg;             
    10.       --判读是否提取到值,没取到值就退出             
    11.       --取到值cur_out_arg%notfound 是false              
    12.       --取不到值cur_out_arg%notfound 是true             
    13.       exit when cur_out_arg%notfound;              
    14.       dbms_output.put_line(rec_arg.id||'-'||rec_arg.name);           
    15.     end loop;         
    16.   --关闭游标        
    17.   close cur_out_arg;  
    18. end;  
     
    自己实例:
    实现效果:通过函数返回结果集表数据

    create type row_type as object(v_date varchar2(50) );--定义行对象
    create type table_type as table of row_type; --定义表对象

    --建立函数

    create or replace function F_TOOLS_GET_DAYS(
    V_DATE IN VARCHAR2
    )
    return table_type pipelined
    is
    v row_type;--定义v为行对象类型
    begin
    for thisrow in (select data_date from (select '20100425' as data_date from dual
    union all
    select '20100426' from dual
    )a
    ) loop
    v := row_type(thisrow.data_date);
    pipe row (v);
    end loop;
    return;
    end;

    --使用函数

    select v_date from table(F_TOOLS_GET_DAYS('201002'));

  • 相关阅读:
    大型项目前端架构浅谈
    图标使用新姿势- react 按需引用 svg 的实现
    为什么json 不能使用 int64类型
    SSL证书对SEO网站优有什么影响?使用HTTPS的SEO优势
    web安全测试必须注意的五个方面
    在小程序中实现 Mixins 方案
    网站web前端常见的优化措施
    前端 HTML空格的六种方式
    如何正确选型,React Native 还是 Native?
    CSS3 渐变(Gradients)
  • 原文地址:https://www.cnblogs.com/xiaojianblogs/p/8073885.html
Copyright © 2011-2022 走看看