oracle函数-动态结果输出
1、oracle函数创建
create or replace function fun_test()begin end fun_test;
2、函数调用
select * from table(fun_test());
3、以结果集返回(仅自己研究的一种方法)
--创建 TYPE 类型 atrr_type
CREATE OR REPLACE TYPE atrr_type FORCE AS OBJECT (orgIdObj varchar2(100),orgNameObj varchar2(100),orgTypeObj VARCHAR2(1)
);
--将 TYPE 类型 atrr_type 定义为表, 用做接收返回值
CREATE OR REPLACE TYPE attr_table AS TABLE of atrr_type;
--以 Table 形式 返回结果集
create or replace function fun_test(orgId in VARCHAR2)return attr_table isattr_row atrr_type; -- 定义单条数据变量attr attr_table := attr_table(); -- 定义返回结果,并初始化beginfor thisrow in (SELECT id as orgIdObj,org_name as orgNameObj,'0' as orgTypeObj FROM sys_org START WITH id ='88a9bed0f9d5bc882e9140feb8f3cfa1' CONNECT BY org_parent_id = PRIOR id) loopattr_row :=atrr_type(thisrow.orgIdObj,thisrow.orgNameObj,thisrow.orgTypeObj);attr.extend;attr(attr.count) := attr_row;--将每一行放到结果中 end loop;return(attr);--返回结果集
end fun_test;
4、调用函数:
select * from table(fun_test('88a9bed0f9d5bc882e9140feb8f3cfa1'));
5、遇到的问题,以及心得
(1)动态sql放到for thisrow in (sql),遍历的过程需要用游标,否则不能取出值
(1.1)定义一个动态游标
TYPE ref_cursor_type IS REF CURSOR; --定义一个动态游标
usrs ref_cursor_type;
(1.2)遍历动态游标
open usrs for sqlSb;--拼接的动态sql,放到游标中进行循环
fetch usrs into orgIdObj,orgNameObj,orgTypeObj,isSub,isItem,isReportType,isCollect,isShowChild;while usrs%foundloop attr_row := atrr_type(orgIdObj,orgNameObj,orgTypeObj,isSub,isItem,isReportType,isCollect,isShowChild);attr.extend;attr(attr.count) := attr_row;fetch usrs into orgIdObj,orgNameObj,orgTypeObj,isSub,isItem,isReportType,isCollect,isShowChild;end loop;close usrs;
(1.3)完整代码块,仅供参考
--条件查询
create or replace function fun_selecttest(orgId in VARCHAR2,params in VARCHAR2)
return attr_table isattr_row atrr_type; -- 定义单条数据变量attr attr_table := attr_table(); -- 定义返回结果,并初始化param0 VARCHAR2(100);param1 VARCHAR2(100);param2 VARCHAR2(100);param3 VARCHAR2(100);param4 VARCHAR2(100);param5 VARCHAR2(100);sqlSb VARCHAR2(2000);--sql 拼接条件TYPE ref_cursor_type IS REF CURSOR; --定义一个动态游标 usrs ref_cursor_type; orgIdObj varchar2(100);orgNameObj varchar2(100);orgTypeObj VARCHAR2(1);isSub Char(1);isItem Char(1);isReportType Char(1);isCollect VARCHAR2(3);isShowChild Char(1);beginDBMS_OUTPUT.ENABLE(buffer_size => null);param0:=substr(params,1,1);--角色: 0股份,1集团,2工程公司,3分公司param1:=substr(params,2,1);--是否分包:0否:集团内分包,1是:集团内及集团外 ,(3组织没有)param2:=substr(params,3,1);--是否显示明细:0否,1是(3组织没有)param3:=substr(params,4,1);--报表类型:0境内,1境外,2全部 (3组织没有)param4:=substr(params,5,1);--汇总产业:0工程承包,1绿色环保,2投资运营,(3组织),4全部param5:=substr(params,6,1);--显示下级所有:0不显示,1显示dbms_output.put_line(param0||param1||param2||param3||param4||param5);--拼接动态sqlsqlSb:='select * from table(fun_alltest('''||orgId||''','''||param5||''')) where 1=1';-- 第二个单引号被作为转义符,第三个单引号被转义if param0='0' then if param5='1' then if param2='1' then sqlSb:=sqlSb||' and (isShowChild =1 or isShowChild =2)';--工程公司以及下属公司和项目elsesqlSb:=sqlSb||' and isShowChild =1';--只查工程公司end if;end if;if param1='0' then sqlSb:=sqlSb||' and (isSub =3 or isSub=1)';end if;if param1='1' then sqlSb:=sqlSb||' and (isSub =3 or isSub=1 or isSub=0)';end if;if (param2='0' or param2='1') then sqlSb:=sqlSb||' and (isItem=3 or isItem='||param2||')';end if;if (param3='0' or param3='1') then sqlSb:=sqlSb||' and (isReportType =3 or isReportType='||param3||')';end if;if param3='2' then sqlSb:=sqlSb||' and (isReportType =3 or isReportType=0 or isReportType=1)';end if;if param4='0' then sqlSb:=sqlSb||' and (isCollect ='||'''3'''||' or isCollect='||'''001'''||')';end if;if param4='1' then sqlSb:=sqlSb||' and (isCollect ='||'''3'''||' or isCollect='||'''002'''||')';end if;if param4='2' then sqlSb:=sqlSb||' and (isCollect ='||'''3'''||' or isCollect='||'''003'''||')';end if;elseif param1='0' then sqlSb:=sqlSb||' and (isSub =3 or isSub=1)';end if;if param1='1' then sqlSb:=sqlSb||' and (isSub =3 or isSub=1 or isSub=0)';end if;if (param2='0' or param2='1') then sqlSb:=sqlSb||' and (isItem=3 or isItem='||param2||')';end if;if (param3='0' or param3='1') then sqlSb:=sqlSb||' and (isReportType =3 or isReportType='||param3||')';end if;if param3='2' then sqlSb:=sqlSb||' and (isReportType =3 or isReportType=0 or isReportType=1)';end if;if param4='0' then sqlSb:=sqlSb||' and (isCollect ='||'''3'''||' or isCollect='||'''001'''||')';end if;if param4='1' then sqlSb:=sqlSb||' and (isCollect ='||'''3'''||' or isCollect='||'''002'''||')';end if;if param4='2' then sqlSb:=sqlSb||' and (isCollect ='||'''3'''||' or isCollect='||'''003'''||')';end if;end if;dbms_output.put_line('sql:'||sqlSb);open usrs for sqlSb;--拼接的动态sql,放到游标中进行循环fetch usrs into orgIdObj,orgNameObj,orgTypeObj,isSub,isItem,isReportType,isCollect,isShowChild;while usrs%foundloop attr_row := atrr_type(orgIdObj,orgNameObj,orgTypeObj,isSub,isItem,isReportType,isCollect,isShowChild);attr.extend;attr(attr.count) := attr_row;fetch usrs into orgIdObj,orgNameObj,orgTypeObj,isSub,isItem,isReportType,isCollect,isShowChild;end loop;close usrs; return(attr);
end fun_selecttest;
oracle函数-动态结果输出相关推荐
- oracle索引自增函数,oracle生成动态前缀且自增号码的函数分享
create or replace Function GetInvitationNO(prev varchar2, num1 varchar2, num2 varchar2, sessionSetti ...
- Oracle EBS 动态调用 XML Publisher 模板 输出不同的报表
Oracle EBS 动态调用 XML Publisher 模板 输出不同的报表 if fnd_request.add_layout(template_appl_name => 'CUX', ...
- 通过GetProcAddress函数动态调用dll中地函数,是否必须通过extern C声明导出函数?(转)...
通过GetProcAddress函数动态调用dll中的函数,是否必须通过extern "C"声明导出函数? [已结贴,结贴人:darongtou] 如题,网上搜了N多资料,一直找不 ...
- php gd库 函数 建立gif,PHP_PHP GD库生成图像的几个函数总结,使用GD库中提供的函数动态绘 - phpStudy...
PHP GD库生成图像的几个函数总结 使用GD库中提供的函数动态绘制完成图像以后,就需要输出到浏览器或者将图像保存起来.在PHP中,可以将动态绘制完成的画布,直接生成GIF.JPEG.PNG和WBMP ...
- jdbc oracle 函数,Oracle系列:(33)JDBC访问Oracle的存储过程和存储函数
1.存储过程 1.1.准备SQL-- 定义存储过程 create or replace procedure get_rax(salary in number,rax out number) as -- ...
- BayaiM__ oracle函数_01
BayaiM__ oracle函数_01 Oracle函数 ---------------------------------------------------------------------- ...
- GetProcAddress()函数动态调用DLL中的函数,是否必须通过extern C声明导出函数?
GetProcAddress()函数动态调用DLL中的函数,是否必须通过extern C声明导出函数? 通过GetProcAddress函数动态调用dll中的函数,是否必须通过extern " ...
- DBA必知的170张Oracle常用动态性能表介绍
DBA必知的170张Oracle常用动态性能表介绍 常用动态性能表.pdf 附录C 动态性能(V$)视图 本附录介绍动态性能视图.这些视图一般作为V$视图引用.本附录包括下列内容: ???? 动态性 ...
- oracle创建编号函数,oracle函数初次尝试
初次着手写function,确实费了好长时间,一个if就需要有个end if ,并且else必须和if一起使用,可以直接对传进来的参数使用oracle自带的函数进行处理,并直接做做判断条件. 需求根 ...
最新文章
- oracle 主键_Oracle约束详解
- 刚刚更新:在线聊天系统设计(原理+思路+源码+效果图)
- gcc,cc,g++,CC的区别
- python数据字符_python数据清洗系列之字符串处理详解
- java反射快速入门(二)
- centos网络隔一段时间就断_潮汕青年说 | 瞎吃小哥:隔着屏幕也要把你看馋的顶配版吃货...
- MySQL 开启慢查询日志
- Jenkins集群搭建
- 怎么一键重装系统?装机大师一键重装图文教程
- 华为路由交换学习篇-VLAN虚拟局域网
- 计算机控制环境下审计风险研究,计算机审计风险研究
- 2016-2017 ACM-ICPC, South Pacific Regional Contest (SPPC 16)
- 招商银行笔试题之修塔游戏
- Lmp7721通过跨阻运放方式采集荧光信号踩坑
- unity如何插入图片_unity 图片导入及其使用方法
- 关于bat中set /p=前面加空格的bug修复方法
- 小屁孩的超可爱新年祝福铃声 小屁孩的超可爱新年祝福手机铃声...
- IDEA导入jar包,及import jar包时jar包自动消失问题解决方法
- 【文献翻译】Prediction of Seizure Recurrence. A Note of Caution
- 直销系统模式开发流程详解