zoukankan      html  css  js  c++  java
  • Oracle的函数返回表类型(转)

    在SQL Server中有表变量,可以在function中方便地返回,习惯SQL Server或者需要把脚本从SQL Server转到Oracle中的朋友可以都会碰到这个问题.

    Oracle的function中怎么返回表变量?

    1、创建表对象类型。

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

    create or replace type t_table is table of number;

    上面的类型定义好后,在function使用可用返回一列的表,如果需要多列的话,需要先定义一个对象类型。然后把对象类型替换上面语句中的number;

    定义对象类型:

    create or replace type obj_table as object
    (
      id int,
      name varchar2(50)
    )

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

    create or replace type t_table is table of obj_table;

     

    2、 创建演示函数

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

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

    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(i*i));
        pipe   row(v_obj_table);   
    end loop;
    return;
    end f_pipe;

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

    调用函数的方式如下:

    select * from table(f_pipe(5));

     

    2)、 普通的方式:

    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;

    初始化值后还可以想注视行那样进行修改.

    调用方式如下:

    select * from table(f_normal(5));

     

     

  • 相关阅读:
    golang中channels的本质详解,经典!
    Vim tips——Working with external commands
    go语言中log包的使用
    vue自定义轮播图组件 swiper
    Nerv --- React IE8 兼容方案
    微信小程序实战之 pay(支付页面)
    微信小程序实战之 goods(订餐页)
    微信小程序 自定义组件(stepper)
    微信小程序 自定义组件(modal) 引入组件
    微信小程序之 Swiper(轮播图)
  • 原文地址:https://www.cnblogs.com/chriskwok/p/4506179.html
Copyright © 2011-2022 走看看