存储过程书写规范

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

ORACLE存储过程编码规范
1.1 变量规范
? 变量名全部采用小写,局部变量名使用“v_”开头,输入参数以“i_开头,输出参数以“o_”开头,输入输出参数用io_开头。所有输入参数必须显示声明
? 游标的命名:游标统一用后缀 “_cur” 命名
? 使用命名的常量以避免硬编码,使用常量包,常量统一用 cn_ 的前缀命名
? 当变量代表列时,使用%TYPE属性,当变量实际上表示数据库表的某列数据时,为避免数据库结构修改对变量的影响,应统一使用%TYPE属性对变量命名
? 使用%TYPE以标准化非数据库变量的声明
1.2 包规范
? 按照项目制定的文件组织划分包内容
? 包内的存储过程及函数的命名必须遵循CPIC制订的相关规范
1.3 游标规范
? 外部查询的多行数据返回使用游标进行处理,通过传递游标变量的形式返回数据到外部接口,由外部程序自行FETCH数据。
? 打开游标前,必须显式检查游标的%ISOPEN属性。
? 使用FETCH语句后,要立即检查%NOTFOUND属性,以便正常终止游标FETCH循环。
? 无论PL/SQL程序是正常终止还是出错退出,都要关闭所有已打开的游标。在出错退出时,应该在其异常处理部分管理所有游标,这可以释放一部分的系统资源
? 尽可能使用显示游标,避免使用隐式游标。
1.4 事务处理规范
? 在需要分割事务以使主事务的提交或者回滚独立于子事务的提交及回滚时,应使用自治事务
? 所有的存储过程均统一在结束处统一COMMIT或者ROLLBACK
1.5 数据封装规范
? 按照业务逻辑实现功能模块的封装,将业务规则逻辑集中在更少量的、良好设计的、易于维护的函数或者过程中,不必在每条SQL语句或者每天PL/SQL程序中重复这些逻辑
? 基于单一数据表的增、删、改、查采用标准SP进行封装,不允许相同逻辑的处理出现在多个SP中
1.6 数据访问规范
? 后台数据按照逻辑划分成多个SCHEMA,不同SCHEMA的数据不可互相访问
? 需要相互访问的表均存放在 ***的SCHEMA中,通过访问***中的接口表实现跨SCHEMA的数据访问
1.7 日志书写规范
? 采用公共的API包完成后台日志数据记录。(API完成输出错误信息提示、记录错误信息内容到数据库表、系统级的错误代码及错误信息等)
? 后台日志的信息记录级别包括INFO、WARN、ERROR,其定义以及不同级别日志的采集标准如下:
n INFO-提示信息,供开发人员调试使用,由开发人员自行确定,主要是调试信息,程序运行中普通信息提示。
n WARN-警告信息,可能导致严重错误的警告信息
n

ERROR-错误信息,导致系统运行错误的信息。
? 所有表操作的错误处理部分均应记录日志信息
1.8 错误处理规范
? 凡是涉及到表操作(insert,update,select,delete)的sql语句,都必须进行错误捕捉,不能将错误带到后面的语句
? 从表中select数据的语句,应严格区分NO_DATA_FOUND 和 TOO_MANY_ROWS的错误,并将相应错误信息填入错误信息
? NO_DATA_FOUND 多数为查询条件问题导致无数据返回(用户级错误)
? TOO_MANY_ROWS 应该是数据表内数据异常导致(系统级错误)
? 所有存储过程(函数)的统一出口一律在存储过程的结束部分,不允许中间返回
? 在每一个异常处理部分,都要定义WHEN OTHER 子程序,以便捕获所有没有显示处理或其他类型的异常
? 所有程序中捕获到的错误,均转换成对应的errcode,errmsg,通过输出参数返回给调用者,所有存储过程(函数)结束前应统一捕获系统异常
? 在每个存储过程(函数)的入口处统一先将返回错误代码(errCode)设置为42,功能处理成功结束后再将错误代码(errCode)设置为0(成功),避免程序过程中因错误未能正确捕获,导致功能未能完成,而程序却成功返回的情况出现
? 所有的模块都有错误编码区间,原则上错误编码全局唯一
? 错误信息描述应准确,业务相关的错误应将输入数据拼接到错误信息中。
? 错误代码具体规范见《建行ODS项目ETL-2.1开发规范(公共).doc》
1.9 书写规范
? PL/SQL语句的所有表名、字段名遵照数据字典的定义,系统保留字、内置函数名、PL/SQL保留字、关键字大写,用户声明的标识符小写。
? 对于子程序、触发器、包等带名的程序块,使用结束标识。
? 变量名小写,局部变量名使用“v_”开头,输入参数以“i_”开头,输出参数以“o_”开头,输入输出参数用io_开头。所有输入参数必须显示声明。
? 连接符OR、IN、AND、以及=、<=、>=等前后加上一个空格。
? 对较为复杂的SQL语句加上注释,说明算法、功能。
? 注释风格:注释单独成行、放在语句前面。
n 应对不易理解的分支条件表达式加注释;
n 对重要的计算应说明其功能;
n 过长的函数实现,应将其语句按实现的功能分段加以概括性说明;
n 常量及变量注释时,应注释被保存值的含义(必须),合法取值的范围
n 可采用单行/多行注释。(-- 或 方式)

? SQL语句的缩进风格
n 一行有多列,超过80个字符时,基于列对齐原则,采用下行缩进
n WHERE子句书写时,每个条件占一行,语句另起一行时,以保留字或者连接符开始

,连接符右对齐。例如:
where f1 = 1
and f2 = 2
or f3 = 3
n INSERT语句,必须书写字段,字段可5个或6个一组。中间用TAB分开
? 多表连接时,使用表的别名来引用列。
? 供别的文件或函数调用的函数,绝不应使用全局变量交换数据;
? TAB 统一定义为4个空格,建议使用Ultraedit作为SQL书写工具
1.10 书写优化性能建议
? 避免嵌套连接。例如:A = B AND B = C AND C = D
? WHERE条件中尽量减少使用常量比较,改用主机变量
? 系统可能选择基于规则的优化器,所以将结果集返回数据量小的表作为驱动表(FROM后边最后一个表)。
? 大量的排序操作影响系统性能,所以尽量减少ORDER BY和GROUP BY排序操作。 如必须使用排序操作,请遵循如下规则
n 排序尽量建立在有索引的列上。
n 如结果集不需唯一,使用UNION ALL代替UNION。
? 索引的使用
n 尽量避免对索引列进行计算。
n 尽量注意比较值与索引列数据类型的一致性。
n 对于复合索引,SQL语句必须使用主索引列
n 索引中,尽量避免使用NULL。
n 对于索引的比较,尽量避免使用!= 查询列和排序列与索引列次序保持一致
? 尽量避免相同语句由于书写格式的不同,而导致多次语法分析。
? 尽量使用共享的SQL语句。
? 查询的WHERE过滤原则,应使过滤记录数最多的条件放在最前面。
? 任何对列的操作都将导致表扫描,它包括数据库函数、计算表达式等等,查询时要尽可能将操作移至等号右边。
? IN、OR子句常会使用工作表,使索引失效;如果不产生大量重复值,可以考虑把子句拆开;拆开的子句中应该包含索引。
1.11 其他经验性规则
? 尽量少用嵌套查询。如必须,请用not exist代替not in子句。
错误
SELECT ......
FROM emp
WHERE dept_no NOT IN ( SELECT dept_no
FROM dept
WHERE dept_cat='A');
正确
SELECT ......
FROM emp e
WHERE NOT EXISTS ( SELECT 'X'
FROM dept
WHERE dept_no=e.dept_no
AND dept_cat='A');


? 用多表连接代替EXISTS子句。
错误
SELECT ......
FROM emp
WHERE EXISTS ( SELECT 'X'
FROM dept
WHERE dept_no=e.dept_no
AND dept_cat='A');
正确
SELECT ......
FROM emp e,dept d
WHERE e.dept_no=d.dept_no
AND dept_cat='A';

? 少用DISTINCT,用EXISTS代替
错误
SELECT DISTINCT d.dept_code,d.dept_name
FROM dept d ,emp e
WHERE e.dept_code=d.dept_code;
正确
SELECT dept_code,dept_name
FROM dept d
WHERE EXISTS ( SELECT 'X'
FROM emp e
WHERE e.dept_code=d.dept_code);


? 使用UNION ALL、MINUS、INTERSECT提高性能
? 使用ROWID提高检索速

度。对SELECT得到的单行记录,需进行DELETE、UPDATE操作时,使用ROWID将会使效率大大提高。
? 使用优化线索机制进行访问路径控制。
? 使用cursor时,显示光标优于隐式光标 。

一:
CREATE OR REPLACE PROCEDURE proc_batch
IS
interactionhour varchar(100);
upcdrname varchar(100);
part_hour varchar(100);
calendar date;
interactionday1 varchar(100);
interactionday varchar(100);
part_day varchar(100);
errmsg varchar(300);
BEGIN
calendar := sysdate-1/24;
part_hour :=to_char(sysdate-1/24,'hh24');
part_day :=to_char(sysdate-1,'dd');
interactionhour := 'interactionhour'||to_char(calendar,'yyyymm');
interactionday := 'interactionday'||to_char(calendar,'yyyymm');
interactionday1 := 'interactionday1'||to_char(calendar,'yyyymm');
upcdrname := 'upcdr'||to_char(calendar,'yymmdd');
proc_interactionhour (interactionhour,upcdrname ,part_hour ,calendar);
if to_char(sysdate,'hh24')='03' then
proc_interactionday (interactionhour ,interactionday ,interactionday1 ,part_day );
end if;
if to_char(sysdate,'hh24')='04' then
proc_interactiondayshow (interactionday1 ,interactionday );
end if;
insert into appmsg
values('成功执行proc_batch','proc_batch',sysdate);
commit;
EXCEPTION

WHEN OTHERS THEN
rollback;
errmsg:= substr(sqlerrm,1,300);
insert into appmsg
values ('没有成功执行proc_batch','proc_batch',sysdate);
commit;

END proc_batch;
/
二:CREATE OR REPLACE PROCEDURE proc_interactionday (interactionhour varchar,interactionday varchar,interactionday1 varchar,part_day varchar)
IS
sqltxt1 varchar(2000);
sqltxt2 varchar(2000);
cur_no number;
cur_val number;
errmsg varchar(300);
BEGIN
sqltxt1 :=' insert into '
|| interactionday
||' (rival_no,area_no,calendar) '
||' select distinct rival_no,area_no,substr(calendar,1,8) '
||' calendar from '
|| interactionhour
||' partition (part_'
|| part_day
||' ) '
||' where 1=1 '
||' and substr(rival_no,1,2)=''04'' ';
cur_no:=dbms_sql.open_cursor;
dbms_sql.parse(cur_no,sqltxt1,dbms_sql.native);
cur_val:=dbms_sql.execute(cur_no);
dbms_sql.close_cursor(cur_no);

sqltxt2 :=' insert into '
|| interactionday1
||' (rival_no,area_no,calendar) '
||' select distinct rival_no,area_no,substr(calendar,1,8) '
||' calendar from '
|| interactionhour
||' partition (part_'
|| part_day
||' ) '
||' where 1=1 '
||' and substr(rival_no,1,3) in (''130'',''131'',''132'',''133'',''134'' ) ';
cur_no:=dbms_sql.open_cursor;
dbms_sql.parse(cur_no,sqltxt2,dbms_sql.native);
cur_val:=dbms_sql.execute(cur_no);
dbms_sql.close_cursor(cur_no);




insert into appmsg
values('成功执行proc_interactionday','proc_interactionday',sysdate);
commit;
EXCEPTION

WHEN OTHERS THEN
rollback;
errmsg := substr(sqlerrm,1,300);
insert into appmsg
values('没有成功执行proc_interactionday,原因是:'||errmsg,'proc_interactionday',sysdate) ;
commit;
END proc_interactionday;
/


三:CREATE OR REPLACE PROCEDURE proc_interactiondayshow (interactionday1 varchar,interactionday varchar)
IS
sqltxt1 varchar(2000);
sqltxt2 varchar(2000);
sqltxt3 varchar(2000);
sqltxt4 varchar(2000);
cur_no number;
cur_val number;
errmsg varchar(300);
BEGIN
sqltxt1 :=' insert into '
||' interactioncodedayshow(supplier,code,tos,area_name,part_name,total,calendar) '
||' select b.supplier, b.code,b.tos,b.area_name,b.part_name,count(*) total,a.calendar '
||' from '
|| interactionday1
||' a, '
||'static_interactionno1 b '
||' where 1=1 '
||' and to_number(substr(a.rival_no,1,7))=b.code '
||' group by b.code,b.supplier,b.tos,b.area_name,b.part_name,a.calendar ';

cur_no:=dbms_sql.open_cursor;
dbms_sql.parse(cur_no,sqltxt1,dbms_sql.native);
cur_val:=dbms_sql.execute(cur_no);
dbms_sql.close_cursor(cur_no);
sqltxt2 :=' insert into '
||'interactioncodedayshow(supplier,area_name,part_name,tos,code,total,calendar)'
||' select c.supplier,c.area_name,c.part_name,c.tos,c.code,d.total,d.calendar '
||' from static_interactionno c, '
||' (select substr(rival_no,1,6) code,count(*) total,calendar '
||' from '
|| interactionday
||' group by substr(rival_no,1,6),calendar '
||' union all '
||' select substr(rival_no,1,7) code,count(*) total,calendar '
||' from '
|| interactionday
||' group by substr(rival_no,1,7),calendar '
||' union all '
||' select substr(rival_no,1,8) code,count(*) total,calendar '
||' from '
|| interactionday
||' group by substr(rival_no,1,8),calendar) d '
||' where c.area_no||c.code=d.code '
||' order by c.area_no ';
cur_no:=dbms_sql.open_cursor;
dbms_sql.parse(cur_no,sqltxt2,dbms_sql.native);
cur_val:=dbms_sql.execute(cur_no);
dbms_sql.close_cursor(cur_no);
sqltxt3 :='insert into'
||' interactionpartdayshow (supplier,area_name,part_name,tos, total,calendar )'
||' select supplier,area_name,part_name,tos,sum(total) total,calendar '
||'from'
||' interactioncodedayshow '
||' group by part_name,supplier,tos,calendar,area_name ';
cur_no:=dbms_sql.open_cursor;
dbms_sql.parse(cur_no,sqltxt3,dbms_sql.na

tive);
cur_val:=dbms_sql.execute(cur_no);
dbms_sql.close_cursor(cur_no);
sqltxt4 :=' insert into'
||' interactionareadayshow (supplier,area_name,tos, total,calendar) '
||' select supplier,area_name,tos,sum(total) total,calendar '
||' from interactioncodedayshow '
||' group by supplier,tos,calendar,area_name ';
cur_no:=dbms_sql.open_cursor;
dbms_sql.parse(cur_no,sqltxt4,dbms_sql.native);
cur_val:=dbms_sql.execute(cur_no);
dbms_sql.close_cursor(cur_no);
insert into appmsg
values('成功执行proc_interactiondayshow','proc_interactiondayshow',sysdate);
commit;

EXCEPTION

WHEN OTHERS THEN
rollback;
errmsg := substr(sqlerrm,1,300);
insert into appmsg
values('没有成功执行proc_interactionday,原因是:'||errmsg,'proc_interactionday',sysdate) ;
commit;
END proc_interactiondayshow;
/

四:CREATE OR REPLACE PROCEDURE proc_interactionhour (interactionhour varchar,upcdrname varchar,part_hour varchar,calendar date)
IS
sqltxt varchar(20000);
cur_no number;
cur_val number;
errmsg varchar(300);
BEGIN
sqltxt :=' insert into '
||interactionhour
||' (rival_no,area_no,calendar) '
||' with '
||' ls1 as '
||' (select case '
||' when substr(trim(truncatedcaller),1,2)=''13'' and length(trim(truncatedcaller))=11 then trim(truncatedcaller) '
||' when substr(trim(truncatedcaller),1,3)=''153'' and length(trim(truncatedcaller))=11 then trim(truncatedcaller) '
||' when substr(trim(truncatedcaller),1,1) between 2 and 8 and length(trim(truncatedcaller))=7 then trim(truncatedcaller) '
||' else ''110'''
||' end rival_no,'
||' case '
||' when substr(trim(applix_no),1,2)=''13'' and length(trim(applix_no))=11 then trim(applix_no) '
||' when substr(trim(applix_no),1,3)=''153'' and length(trim(applix_no))=11 then trim(applix_no) '
||' when substr(trim(applix_no),1,1)=''0'' and length(trim(applix_no))=11 then trim(applix_no) '
||' when substr(trim(applix_no),1,2) between 2 and 8 and length(trim(applix_no))=7 then trim(applix_no) '
||' else ''110'''
||' end applix_no,sarea,darea,'
|| to_char(calendar,'yyyymmddhh24')
||' calendar from '
||upcdrname
||' partition (part_'
||part_hour
||') cdr '
||' where 1=1'
||' and exists (select ''x'' from static_interactionname s where cdr.opc=s.xpc and netname<>''移动'')'

||' and exists (select ''x'' from static_interactionname s where cdr.dpc=s.xpc and netname=''移动'')'
||' and cf between 132 and 139 '
||' ), '
||' ls2 as '
||' (select case '
||' when substr(trim(truncatedcalled),1,2)=''13'' and length(trim(truncatedcalled))=11 then trim(truncatedcalled) '
||' when substr(trim(truncatedcalled),1,3)=''153'' and length(trim(truncatedcalled))=11 then trim(truncatedcalled) '
||' when substr(trim(truncatedcalled),1,1) between 2 and 8 and length(trim(truncatedcalled))=7 then trim(truncatedcalled) '
||' else ''110'''
||' end rival_no,'
||' case '
||' when substr(trim(applix_no),1,2)=''13'' and length(trim(applix_no))=11 then trim(applix_no) '
||' when substr(trim(applix_no),1,3)=''153'' and length(trim(applix_no))=11 then trim(applix_no) '
||' when substr(trim(applix_no),1,1)=''0'' and length(trim(applix_no))=11 then trim(applix_no) '
||' when substr(trim(applix_no),1,2) between 2 and 8 and length(trim(applix_no))=7 then trim(applix_no) '
||' else ''110'''
||' end applix_no,sarea,darea,'
|| to_char(calendar,'yyyymmddhh24')
||' calendar from '
||upcdrname
||' partition (part_'
||part_hour
||' ) cdr '
||' where 1=1 '
||' and exists (select ''xpc'' from static_interactionname s where cdr.dpc=s.xpc and netname<>''移动'') '
||' and exists (select ''xpc'' from static_interactionname s where cdr.opc=s.xpc and netname=''移动'')'
||' and cf between 132 and 139 '
||' ) '
||' select rival_no,sarea,calendar '
||' from ls1 '
||' where sarea between 431 and 439 '
||' union '
||' select applix_no,darea,calendar '
||' from ls1 '
||' where darea between 431 and 439 '
||' union '
||' select rival_no,darea,calendar '
||' from ls2 '
||' where darea between 431 and 439 '
||' union '
||' select applix_no,darea,calendar '
||' from ls2 '
||' where darea between 431 and 439 ';
cur_no:=dbms_sql.open_cursor;
dbms_sql.parse(cur_no,sqltxt,dbms_sql.native);
cur_val:=dbms_sql.execute(cur_no);
dbms_sql.close_cursor(cur_no);
c

ommit;
insert into appmsg
values('成功执行proc_interactionhour','proc_interactionhour',sysdate);
commit;
EXCEPTION
WHEN OTHERS THEN
rollback;
errmsg := substr(sqlerrm,1,300);
insert into appmsg
values('没有成功执行proc_interactionhour,原因是:'||errmsg,'proc_interactionhour',sysdate) ;
commit;
END proc_interactionhour;
/


五:
CREATE OR REPLACE PROCEDURE proc_interactionhouryzg (interactionhour varchar,upcdrname varchar,part_hour varchar,calendar date)
IS
sqltxt varchar(20000);
cur_no number;
cur_val number;
errmsg varchar(300);
BEGIN
sqltxt :=' insert into '
||interactionhour
||' (rival_no,area_no,calendar) '
||' with '
||' ls1 as '
||' (select case '
||' when substr(trim(truncatedcaller),1,2)=''13'' and length(trim(truncatedcaller))=11 then trim(truncatedcaller) '
||' when substr(trim(truncatedcaller),1,3)=''153'' and length(trim(truncatedcaller))=11 then trim(truncatedcaller) '
||' when substr(trim(truncatedcaller),1,1) between 2 and 8 and length(trim(truncatedcaller))=7 then ''0''||sarea||trim(truncatedcaller) '
||' else ''110'''
||' end rival_no,'
||' case '
||' when substr(trim(applix_no),1,2)=''13'' and length(trim(applix_no))=11 then trim(applix_no) '
||' when substr(trim(applix_no),1,3)=''153'' and length(trim(applix_no))=11 then trim(applix_no) '
||' when substr(trim(applix_no),1,1)=''0'' and length(trim(applix_no))=11 then trim(applix_no) '
||' when substr(trim(applix_no),1,2) between 2 and 8 and length(trim(applix_no))=7 then ''0''||darea||trim(applix_no) '
||' else ''110'''
||' end applix_no,sarea,darea,'
|| to_char(calendar,'yyyymmddhh24')
||' calendar from '
||upcdrname
||' partition (part_'
||part_hour
||') cdr '
||' where 1=1'
||' and exists (select ''x'' from static_interactionname s where cdr.opc=s.xpc and netname<>''移动'')'
||' and exists (select ''x'' from static_interactionname s where cdr.dpc=s.xpc and netname=''移动'')'
||' and cf between 132 and 139 '
||' ), '
||' ls2 as '
||' (select case '
||' when substr(trim(truncatedcalled),1,2)=''13'' and length(trim(truncatedcalled))=11 then trim(truncatedcalled) '
||' when substr(trim(truncatedcalled),1,3)=''153'' and length(trim(

truncatedcalled))=11 then trim(truncatedcalled) '
||' when substr(trim(truncatedcalled),1,1) between 2 and 8 and length(trim(truncatedcalled))=7 then ''0''||darea||trim(truncatedcalled) '
||' else ''110'''
||' end rival_no,'
||' case '
||' when substr(trim(applix_no),1,2)=''13'' and length(trim(applix_no))=11 then trim(applix_no) '
||' when substr(trim(applix_no),1,3)=''153'' and length(trim(applix_no))=11 then trim(applix_no) '
||' when substr(trim(applix_no),1,1)=''0'' and length(trim(applix_no))=11 then trim(applix_no) '
||' when substr(trim(applix_no),1,2) between 2 and 8 and length(trim(applix_no))=7 then ''0''||darea||trim(applix_no) '
||' else ''110'''
||' end applix_no,sarea,darea,'
|| to_char(calendar,'yyyymmddhh24')
||' calendar from '
||upcdrname
||' partition (part_'
||part_hour
||' ) cdr '
||' where 1=1 '
||' and exists (select ''xpc'' from static_interactionname s where cdr.dpc=s.xpc and netname<>''移动'') '
||' and exists (select ''xpc'' from static_interactionname s where cdr.opc=s.xpc and netname=''移动'')'
||' and cf between 132 and 139 '
||' ) '
||' select rival_no,sarea,calendar '
||' from ls1 '
||' where sarea between 431 and 439 '
||' union '
||' select applix_no,darea,calendar '
||' from ls1 '
||' where darea between 431 and 439 '
||' union '
||' select rival_no,darea,calendar '
||' from ls2 '
||' where darea between 431 and 439 '
||' union '
||' select applix_no,darea,calendar '
||' from ls2 '
||' where darea between 431 and 439 ';
cur_no:=dbms_sql.open_cursor;
dbms_sql.parse(cur_no,sqltxt,dbms_sql.native);
cur_val:=dbms_sql.execute(cur_no);
dbms_sql.close_cursor(cur_no);
commit;
insert into appmsg
values('成功执行proc_interactionhour','proc_interactionhour',sysdate);
commit;
EXCEPTION
WHEN OTHERS THEN
rollback;
errmsg := substr(sqlerrm,1,300);
insert into appmsg
values('没有成功执行proc_interactionhour,原因是:'||errmsg,'proc_interactionhour',sysdate) ;
commit;
END proc_interactionhouryzg;
/

六:
CREATE OR REPLACE PROCEDURE proc_text(interactionhour varchar,upcdrname var

char,part_hour varchar,calendar date)
IS
sqltxt varchar(2000);
cur_no number;
cur_val number;
errmsg varchar(300);
BEGIN
sqltxt :=' insert into '
|| interactionhour
||' (rival_no,area_no,calendar) '
||' with '
||' ls1 as '
||' ('
||' select '
||' case '
||' when substr(trim(truncatedcaller),1,2)=''13'' and length(trim(truncatedcaller))=11 then trim(truncatedcaller) '
||' when substr(trim(truncatedcaller),1,3)=''153'' and length(trim(truncatedcaller))=11 then trim(truncatedcaller) '
||' when substr(trim(truncatedcaller),1,1) between 2 and 8 and length(trim(truncatedcaller))=7 then trim(truncatedcaller) '
||' else ''8688888'' '
||' end rival_no, '
||' case '
||' when substr(trim(applix_no),1,2)=''13'' and length(trim(applix_no))=11 then trim(applix_no) '
||' when substr(trim(applix_no),1,3)=''153'' and length(trim(applix_no))=11 then trim(applix_no) '
||' when substr(trim(applix_no),1,2) between 2 and 8 and length(trim(applix_no))=7 then trim(applix_no) '
||' when substr(trim(applix_no),1,1)=''0'' and length(trim(applix_no))=11 then trim(applix_no) '
||' else ''8688888'' '
||' end applix_no,sarea,darea,''20060902'' '
|| to_char(calendar,'yyyymmddhh24')
||' calendar from '
|| upcdrname
||' partition (part_ '
|| part_hour
||' ) '
||' cdr '
||' where 1=1 '
||' and exists (select ''x'' from static_interactionname e where cdr.opc=e.xpc and netname<>''移动'') '
||' and exists (select ''x'' from static_interactionname e where cdr.dpc=e.xpc and netname=''移动'') '
||' and cf between 132 and 139), '
||' ls2 as '
||' (select case '
||' when substr(trim(truncatedcalled),1,2)=''13'' and length(trim(truncatedcalled))=11 then trim(truncatedcalled) '
||' when substr(trim(truncatedcalled),1,3)=''153'' and length(trim(truncatedcalled))=11 then trim(truncatedcalled) '
||' when substr(trim(truncatedcalled),1,1) between 2 and 8 and length(trim(truncatedcalled))=7 then trim(truncatedcalled) '
||' else ''8688888'' '
||' end rival_no, '
||' case '
||' when substr(trim(applix_no),1,2)=''13'' and length(trim(applix_no))=11 then trim(applix_no) '
||' when substr(trim(applix_no),1,3)=''153'' and length(trim(applix_no))=11 then trim(applix_no) '
||' when substr(trim(applix_no),1,2) between 2 and 8 and length(trim(applix_no))=7 then trim(applix_no) '
||' when substr(trim(applix_no),1,1)=''0'' and length(trim(applix_no))=11 then trim(applix_no) '
||' else ''8688888'' '
||' end applix_no,darea,''20060902'' '
|| to_char(calendar,'yyyym

mddhh24')
||' calendar from '
|| upcdrname
||' partition (part_ '
|| part_hour
||' ) '
||' cdr '
||' where 1=1 '
||' and exists (select ''xpc'' from static_interactionname e where cdr.opc=e.xpc and netname=''移动'') '
||' and exists (select ''xpc'' from static_interactionname e where cdr.dpc=e.xpc and netname<>''移动'') '
||' and cf between 132 and 139) '
||' select rival_no,sarea,calendar '
||' from ls1 '
||' where 1=1 '
||' and sarea between 431 and 439 '
||' union '
||' select applix_no,darea,calendar '
||' from ls1 '
||' where 1=1 '
||' and darea between 431 and 439 '
||' union '
||' select rival_no,darea,calendar '
||' from ls2 '
||' where 1=1 '
||' and darea between 431 and 439 '
||' union '
||' select applix_no,darea,calendar '
||' from ls2 '
||' where 1=1 '
||' and darea between 431 and 439 ';

cur_no:=dbms_sql.open_cursor;
dbms_sql.parse(cur_no,sqltxt,dbms_sql.native);
cur_val:=dbms_sql.execute(cur_no);
dbms_sql.close_cursor(cur_no);

insert into appmsg
values('成功执行proc_test','proc_test',sysdate);
commit;
EXCEPTION
WHEN OTHERS THEN
rollback;
errmsg := substr(sqlerrm,1,300);
insert into appmsg
values('没有成功执行proc_test 原因是:'||errmsg,'proc_test',sysdate) ;
END proc_text;
/

相关文档
最新文档