-- sql 调用 select * from table( get_airway_subpoint(x,x,x))

/////////////////////////////////////////////////////////////////////

方法一、用游标实现,SLM提供,没有后面的方案二好,可以不用游标,更简单的方案2

//////////////////////////////////////////////////////////////////////

create or replace function subairway(pid1 in number,

awid in number,

pid2 in number) return tab_pnt is

--函数功能是返回awid航路两点间的所有航路点

type mycursor is ref cursor; --定义游标类型

dr      row_pnt := row_pnt(0, 0, null); --type 行类型每条点记录 create or replace type row_pnt is object(srt number(5),pid number(11),pnm varchar2(50))

dt      tab_pnt:= tab_pnt();            --type 表类型,返回航路所有点 create or replace type tab_pnt as table of row_pnt

dtt tab_pnt:= tab_pnt(); --结果表,两点之间的所有表记录

-- 表类型和行类型必须先初始化,否则编译不错但运行会提示错误

cur_pnt mycursor; -- 游标

n1 number(5); --两点在航路走向中的序号

n2 number (5);

tm number(5);

begin

open cur_pnt for --游标可看做一张结果表

select st,pid,pnm from (

select f1.code_sort st,f2.airway_point1 pid,f3.txt_name || decode(f3.tab, 'DES', '', f3.tab) pnm

from rte_seg f1, segment f2, airway_point f3

where f1.segment_id = f2.segment_id

and f2.airway_point1 = f3.significant_point_id

and f1.en_route_rte_id = awid

union

select f1.code_sort + 1 st, f2.airway_point2 pid,f3.txt_name || decode(f3.tab, 'DES', '', f3.tab) pnm

from rte_seg f1, segment f2, airway_point f3

where f1.segment_id = f2.segment_id

and f2.airway_point2 = f3.significant_point_id

and f1.code_sort =(select max(code_sort)from rte_seg where rte_seg.en_route_rte_id = f1.en_route_rte_id)

and f1.en_route_rte_id = awid

order by st

);

IF cur_pnt%NOTFOUND THEN --如果游标内没数据,返回空表

RETURN dt;

END IF;

loop

FETCH cur_pnt

INTO dr.srt, dr.pid, dr.pnm;

EXIT WHEN cur_pnt%NOTFOUND; --到达游标结束,这行位置不能放最后,否则会重复一行

dt.extend(); --dt增加一行新行

dt(dt.count()) := dr; --dt最后一行=dr

END LOOP;

if pid1=0 or pid2=0 then --如果输入变量首点id或末点id 0 表示返回所有航路走向

return dt;

end if;

n1:=-1; --初始值,用于判断时候再航路上找到输入点

n2:=-1;

for v in 1..dt.count() loop --从第一行到dt表的最后一行

if pid1 = dt(v).pid then n1:=dt(v).srt; --找到n1 n2对应位置

elsif pid2 = dt(v).pid then n2:=dt(v).srt;

end if;

end loop;

if n1=-1 or n2=-1 then --至少有一点没匹配上,返回空表

return dtt;

end if;

if n1<n2 then --1序号小于点2序号,正常循环

for v in n1..n2 loop

if dt(v).srt >= n1 and dt(v).srt<=n2 then --dt(v).srt 从表结构逐级访问行结构、字段结构

dtt.extend();

dtt(dtt.count()):= dt(v);

end if;

end loop;

end if;

if n1>n2 then --如果n1>n2说明p1,p2顺序颠倒

tm:=n1;

while tm >= n2 LOOP

begin

dtt.extend();

dtt(dtt.count()):= dt(tm);

tm:= tm - 1;

end;

end LOOP;

end if;

return dtt;

end;

////////////////////////////////////////////////////////////////

方案2 不用游标 关键是 for myrow in (select ...) loop 函数 其余与方案一相同

////////////////////////////////////////////////////////////////

create or replace function subairway(pid1 in number,

awid in number,

pid2 in number,

ishis in number default 0 ) return tab_pnt is

--函数功能是返回awid航路两点间的所有航路点,使用此函数获得某航路两点间的所有点后时,不能用srt排序,否则永远只能得到按原航路顺序的点串,而不是按入点到出点顺序的点串。

dr      row_pnt := row_pnt(0, 0, null); --type 行类型 每条点记录 create or replace type row_pnt is object(srt number(5),pid number(11),pnm varchar2(50))

dt      tab_pnt:= tab_pnt();            --type 表类型,返回航路所有点 create or replace type tab_pnt as table of row_pnt

dtt tab_pnt:= tab_pnt(); --结果表,两点之间的所有表记录

-- 表类型和行类型必须先初始化,否则编译不错但运行会提示错误

n1 number(5); --两点在航路走向中的序号

n2 number (5);

tm number(5);

begin

if ishis=1 then --2016.9.7 原来写的临时航路居然找不到了,重写

for myrow in

( select  sort st, airway_point_id pid, airway_point_name pnm from his_airway_pnts

where airway_id=awid

order by sort

)

loop

dr:=row_pnt(myrow.st,myrow.pid,myrow.pnm);

dt.extend(); --dt增加一行新行

dt(dt.count()) := dr; --dt最后一行=dr

END LOOP;

else

for myrow in

(

select st,pid,pnm from (

select f1.code_sort st,f2.airway_point1 pid,f3.txt_name || decode(f3.tab, 'DES', '', f3.tab) pnm

from rte_seg f1, segment f2, airway_point f3

where f1.segment_id = f2.segment_id

and f2.airway_point1 = f3.significant_point_id

and f1.en_route_rte_id = awid

union

select f1.code_sort + 1 st, f2.airway_point2 pid,f3.txt_name || decode(f3.tab, 'DES', '', f3.tab) pnm

from rte_seg f1, segment f2, airway_point f3

where f1.segment_id = f2.segment_id

and f2.airway_point2 = f3.significant_point_id

and f1.code_sort =(select max(code_sort)from rte_seg where rte_seg.en_route_rte_id = f1.en_route_rte_id)

and f1.en_route_rte_id = awid

order by st

)

)

loop

dr:=row_pnt(myrow.st,myrow.pid,myrow.pnm);

dt.extend(); --dt增加一行新行

dt(dt.count()) := dr; --dt最后一行=dr

END LOOP;

end if;

if pid1=0 or pid2=0 then --如果输入变量 首点id或末点id 为0 表示返回所有航路走向

return dt;

end if;

n1:=-1; --初始值,用于判断时候再航路上找到输入点

n2:=-1;

for v in 1..dt.count() loop --从第一行到dt表的最后一行

if pid1 = dt(v).pid then n1:=dt(v).srt; --找到n1 n2对应位置

elsif pid2 = dt(v).pid then n2:=dt(v).srt;

end if;

end loop;

if n1=-1 or n2=-1 then --至少有一点没匹配上,返回空表

return dtt;

end if;

if n1<n2 then --点1序号小于点2序号,正常循环

for v in n1..n2 loop

if dt(v).srt >= n1 and dt(v).srt<=n2 then --dt(v).srt 从表结构逐级访问行结构、字段结构

dtt.extend();

dtt(dtt.count()):= dt(v);

end if;

end loop;

end if;

if n1>n2 then --如果n1>n2说明p1,p2顺序颠倒

tm:=n1;

while tm >= n2 LOOP

begin

dtt.extend();

dtt(dtt.count()):= dt(tm);

dtt(dtt.count()).srt:=n1-tm+1; --2016.9.7 倒序排列时,为了让序号体现真正的从小到大顺序,改写每点序号

tm:= tm - 1;

end;

end LOOP;

end if;

return dtt;

end;

2015.1.15 利用Oracle函数返回表结果 重大技术进步!的更多相关文章

  1. 2015.1.15 利用Oracle函数插入表结构 Bulk collect into 不用循环,简洁高效

    如果表结构只对应一个字段,可以 select col1 bulk collect into 变量,不用游标循环,简洁高效 create or replace function get_airway_s ...

  2. 浅谈Oracle函数返回Table集合

    在调用Oracle函数时为了让PL/SQL 函数返回数据的多个行,必须通过返回一个 REF CURSOR 或一个数据集合来完成.REF CURSOR 的这种情况局限于可以从查询中选择的数据,而整个集合 ...

  3. 160628、利用Oracle rownum让表排序字段值连续

    利用Oracle rownum让表排序字段值连续 1.需求说明 表(eval_index)中有字段如下: 表字段 描述 说明 ID 主键 GROUP_ID 分组编号 SORT_NUM 排序序号 按照分 ...

  4. Oracle的函数返回表类型(转)

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

  5. oracle函数返回结果集

    一.用自定义类型实现 1.创建表对象类型. 在Oracle中想要返回表对象,必须自定义一个表类型,如下所示: create or replace type type_table is table of ...

  6. 2015.1.15 利用航线id取所有点的函数创建视图

    1.根据航路id取所有航路点的函数 create or replace function alinepnts(alid in number) return tab_airline_pnt is --返 ...

  7. C# ODP.NET 调用Oracle函数返回值时报错的一个解决方案

    有人在社区问到:C#调用Oracle中自定义函数的返回值时,无法正常调用.但在PL/SQL中正常调用返回. 于是动手一试: 1.准备函数(Oralce 11g.2.0.0.4) CREATE OR R ...

  8. SQL函数返回表的示例-Z

    create function [dbo].[GetOperateCustGroup] ( ), ) ) returns @TempTable table (MaxPrice float,MinPri ...

  9. oracle pipelined返回值函数 针对数据汇总统计 返回结果集方法

    近期需要一个汇总统计,由于数据太多,数据量太大所以在java程序中实现比较困难.若用后台程序统计,数据不能保证实时,同时实现周期比较长.顾使用函数返回结果集的方式,在不增加临时表的情况下实时获取数据. ...

随机推荐

  1. composer启用国内镜像网站的配置更改办法

    用法: 有两种方式启用本镜像服务: 将以下配置信息添加到 Composer 的配置文件 config.json 中(系统全局配置).见“例1” 将以下配置信息添加到你的项目的 composer.jso ...

  2. 【转载】Java类加载原理解析

    Java类加载原理解析 原文出处:http://www.blogjava.net/zhuxing/archive/2008/08/08/220841.html 1       基本信息 摘要: 每个j ...

  3. Qt之密码框不可全选、复制、粘贴无右键菜单等

    转载---> http://blog.sina.com.cn/s/blog_a6fb6cc90101artk.html 在做用户登录界面的时候,往往会用到密码框,则其中的一些功能也要求与普通的输 ...

  4. hdu 4737 A Bit Fun 尺取法

    A Bit Fun Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Proble ...

  5. uva 11752 The Super Powers 素数+大数判断大小

    题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_proble ...

  6. DelphiXE_Android

    1. http://download.csdn.net/detail/gx15941883020/8104761 2. http://bbs.2ccc.com/topic.asp?topicid=45 ...

  7. Explain分析查询语句

    ​表的读取顺序 读取操作的类型 可用索引,实际使用的索引 表之间的引用 每张表多少行被优化器查询 索引的长度 EXPLAIN字段解释: ØTable:显示这一行的数据是关于哪张表的 Øpossible ...

  8. Gridview中Datakeys 通过主键取得各列的值。

    首先在初始化Gridview时候定义主键的数组. GridViewTeacherStudent.DataKeyNames=new string[] {"courseId",&quo ...

  9. java学习笔记--常用类

    一.Math类:针对数学运算进行操作的类 1.常用的方法 A:绝对值   public static int abs(int a) B:向上取整  public static double ceil( ...

  10. hive 遇到的问题及解决方法

    org.apache.hadoop.ipc.RemoteException(org.apache.hadoop.ipc.StandbyException): Operation category RE ...