项目中Orcale存储过程优化记录
今天对之前写的Orcale存储过程做了一些优化,使其变得更加灵活,之前写的存储过程是使用游标存储SQL执行结果,但是使用游标带来的弊端是,在声明时,它所执行的SQL就已经被写死,无法修改。若想更加灵活,必须将使用游标这种策略舍弃掉,转而使用在“过程”中定义SQL语句的写法:主要思路是利用execute immediate SQL语句 into 自定义成员变量;
执行并存储SQL的结果(单个的),接下来就是拼接这条SQL语句,和JAVA里的SQL拼接套路完全一致。
之前使用游标的存储过程,只能执行代码中写好的SQL语句:
create or replace procedure proc_g_normalcount(GSYSYW OUT varchar2,GSWSYY OUT varchar2,SBHBTMCT OUT varchar2,SBHTMCBT OUT varchar2,SBHMCXT OUT varchar2,GDSBHCF OUT varchar2,GDMCCF OUT varchar2) as
cursor c_postype is
SELECT COUNT(*) FROM TAX_INFO_GD WHERE (SELECT COUNT(1) as num from TAX_INFO WHERE A.NSR = B.NAME AND A.SHXYDM = B.SH_ID) = 0 AND B.STATE = '正常' AND (B.SYLX = '1' OR B.SYLX = '3');
cursor c_postype1 is
SELECT COUNT(*) FROM TAX_INFO B WHERE (SELECT COUNT(1) as num from TAX_INFO_GD A WHERE A.NAME = B.NSR AND A.SH_ID = B.SHXYDM) = 0 AND B.STATE = '正常';
cursor c_postype2 is
select COUNT(NAME) from (select TAX_INFO_GD.NAME, TAX_INFO.NSR TAX_INFO_GD.SH_ID, TAX_INFO.SHXYDM, TAX_INFO_GD.SYLX, TAX_INFO_GD.JDXZ, TAX_INFO_GD.STATE as GDSTATE, TAX_INFO.STATE as SYSTATE FROM TAX_INFO_GD INNER JOIN TAX_INFO ON TAX_INFO_GD.NAME = TAX_INFO.NSR) where SH_ID <> SHXYDM and (SYLX = 1 OR SYLX = 3) and GDSTATE='正常' and SYSTATE='正常';
cursor c_postype3 is
select COUNT(SH_ID) from (select TAX_INFO_GD.NAME, TAX_INFO.NSR, TAX_INFO_GD.SH_ID, TAX_INFO.SHXYDM, TAX_INFO_GD.SYLX, TAX_INFO_GD.JDXZ, TAX_INFO_GD.STATE as GDSTATE, TAX_INFO.STATE as SYSTATE FROM TAX_INFO_GD INNER JOIN TAX_INFO ON TAX_INFO_GD.SH_ID = TAX_INFO.SHXYDM) where NAME <> NSR and (SYLX = 1 OR SYLX = 3) and GDSTATE='正常' and SYSTATE='正常';
cursor c_postype4 is
select COUNT(NAME) from (select TAX_INFO_GD.NAME, TAX_INFO.NSR, TAX_INFO_GD.SH_ID, TAX_INFO.SHXYDM, TAX_INFO_GD.SYLX, TAX_INFO_GD.JDXZ, TAX_INFO_GD.STATE as GDSTATE, TAX_INFO.STATE as SYSTATE FROM TAX_INFO_GD INNER JOIN TAX_INFO ON TAX_INFO_GD.NAME = TAX_INFO.NSR AND TAX_INFO_GD.SH_ID = TAX_INFO.SHXYDM AND (SYLX = 1 OR SYLX = 3)) where GDSTATE='正常' and SYSTATE='正常';
cursor c_postype5 is
SELECT COUNT(*) FROM (SELECT SH_ID FROM TAX_INFO_GD A WHERE SH_ID IN (SELECT SH_ID FROM TAX_INFO_GD B WHERE A.NAME <> B.NAME AND (A.SYLX = 1 OR A.SYLX = 3) AND (B.SYLX = 1 OR B.SYLX = 3)) AND STATE = '正常' GROUP BY SH_ID);
cursor c_postype6 is
SELECT COUNT(*) FROM (SELECT NAME FROM TAX_INFO_GD A WHERE NAME IN (SELECT NAME FROM TAX_INFO_GD B WHERE A.SH_ID <> B.SH_ID AND (A.SYLX = 1 OR A.SYLX = 3) AND (B.SYLX = 1 OR B.SYLX = 3)) AND STATE = '正常' GROUP BY NAME);
begin
open c_postype;
fetch c_postype into GSYSYW;
close c_postype;
open c_postype1;
fetch c_postype1 into GSWSYY;
close c_postype1;
open c_postype2;
fetch c_postype2 into SBHBTMCT;
close c_postype2;
open c_postype3;
fetch c_postype3 into SBHTMCBT;
close c_postype3;
open c_postype4;
fetch c_postype4 into SBHMCXT;
close c_postype4;
open c_postype5;
fetch c_postype5 into GDSBHCF;
close c_postype5;
open c_postype6;
fetch c_postype6 into GDMCCF;
close c_postype6;
end;
改进后的存储过程,多加入两个传入参数USERRIGHT,TAXTYPE ,根据USERRIGHT,TAXTYPE 返回对应的数据条目数:
create or replace procedure proc_d_normalcount(
USERRIGHT IN varchar2,
TAXTYPE IN varchar2,
GDSYSYW OUT varchar2,
GDSWSYY OUT varchar2,
SBHBTMCT OUT varchar2,
SBHTMCBT OUT varchar2,
SBHMCXT OUT varchar2,
GDSBHCF OUT varchar2,
GDMCCF OUT varchar2) as
s_sql1 varchar2(1000) := '';
s_sql2 varchar2(1000) := '';
s_sql3 varchar2(1000) := '';
s_sql4 varchar2(1000) := '';
s_sql5 varchar2(1000) := '';
s_sql6 varchar2(1000) := '';
s_sql7 varchar2(1000) := '';
v_urstr varchar2(100) := '';
v_urstr2 varchar2(100) := '';
begin
if USERRIGHT='0' then
v_urstr := ' AND 0=1';
v_urstr2 := ' AND 0=1';
else if USERRIGHT='1' then
v_urstr := ' AND 1=1';
v_urstr2 := ' AND 1=1';
else
v_urstr := ' AND B.JDXZ='''||USERRIGHT||'''';
v_urstr2 := ' AND JDXZ='''||USERRIGHT||'''';
end if;
end if;
--国或地税有税源无
s_sql1 := 'SELECT COUNT(*) FROM TAX_INFO_GD B WHERE (SELECT COUNT(1) as num from TAX_INFO A WHERE A.NSR = B.NAME AND A.SHXYDM = B.SH_ID) = 0 AND B.STATE = ''正常'' AND (B.SYLX = '''||TAXTYPE||''' OR B.SYLX =''3'')'||v_urstr;
execute immediate s_sql1 into GDSYSYW;
--国或地税无税源有
s_sql2 := 'SELECT COUNT(*) FROM TAX_INFO B WHERE (SELECT COUNT(1) as num from TAX_INFO_GD A WHERE A.NAME = B.NSR AND A.SH_ID = B.SHXYDM) = 0 AND B.STATE = ''正常''';
execute immediate s_sql2 into GDSWSYY;
--识别号不同名称同
s_sql3 := 'select COUNT(NAME) from (select TAX_INFO_GD.NAME,TAX_INFO.NSR,TAX_INFO_GD.SH_ID,TAX_INFO.SHXYDM,TAX_INFO_GD.SYLX,TAX_INFO_GD.JDXZ,TAX_INFO_GD.STATE as GDSTATE,TAX_INFO.STATE as SYSTATE FROM TAX_INFO_GD INNER JOIN TAX_INFO ON TAX_INFO_GD.NAME = TAX_INFO.NSR) B where SH_ID <> SHXYDM and (SYLX = '||TAXTYPE||' OR SYLX = 3) and GDSTATE=''正常''and SYSTATE=''正常'''||v_urstr2;
execute immediate s_sql3 into SBHBTMCT;
--识别号同名称不同
s_sql4 := 'select COUNT(SH_ID) from (select TAX_INFO_GD.NAME, TAX_INFO.NSR, TAX_INFO_GD.SH_ID, TAX_INFO.SHXYDM, TAX_INFO_GD.SYLX, TAX_INFO_GD.JDXZ, TAX_INFO_GD.STATE as GDSTATE, TAX_INFO.STATE as SYSTATE FROM TAX_INFO_GD INNER JOIN TAX_INFO ON TAX_INFO_GD.SH_ID = TAX_INFO.SHXYDM) where NAME <> NSR and (SYLX = '||TAXTYPE||' OR SYLX = 3) and GDSTATE=''正常'' and SYSTATE=''正常'''||v_urstr2;
execute immediate s_sql4 into SBHTMCBT;
--识别号名称均相同
s_sql5 := 'select COUNT(NAME) from (select TAX_INFO_GD.NAME, TAX_INFO.NSR, TAX_INFO_GD.SH_ID, TAX_INFO.SHXYDM, TAX_INFO_GD.SYLX, TAX_INFO_GD.JDXZ, TAX_INFO_GD.STATE as GDSTATE, TAX_INFO.STATE as SYSTATE FROM TAX_INFO_GD INNER JOIN TAX_INFO ON TAX_INFO_GD.NAME = TAX_INFO.NSR AND TAX_INFO_GD.SH_ID = TAX_INFO.SHXYDM AND (SYLX = '||TAXTYPE||' OR SYLX = 3)) where GDSTATE=''正常'' and SYSTATE=''正常'''||v_urstr2;
execute immediate s_sql5 into SBHMCXT;
--国或地税识别号重复
s_sql6 := 'SELECT COUNT(*) FROM (SELECT SH_ID FROM TAX_INFO_GD A WHERE SH_ID IN (SELECT SH_ID FROM TAX_INFO_GD B WHERE A.NAME <> B.NAME AND (A.SYLX = '||TAXTYPE||' OR A.SYLX = 3) AND (B.SYLX = '||TAXTYPE||' OR B.SYLX = 3) AND A.STATE = ''正常'' AND B.STATE = ''正常'') '||v_urstr2||' GROUP BY SH_ID)';
execute immediate s_sql6 into GDSBHCF;
--国或地名称重复
s_sql7 := 'SELECT COUNT(*) FROM (SELECT NAME FROM TAX_INFO_GD A WHERE NAME IN (SELECT NAME FROM TAX_INFO_GD B WHERE A.SH_ID <> B.SH_ID AND (A.SYLX = '||TAXTYPE||' OR A.SYLX = 3) AND (B.SYLX = '||TAXTYPE||' OR B.SYLX = 3) AND A.STATE = ''正常'' AND B.STATE = ''正常'') '||v_urstr2||' GROUP BY NAME)';
execute immediate s_sql7 into GDMCCF;
end;
项目中Orcale存储过程优化记录的更多相关文章
- 项目中oracle存储过程记录——经常使用语法备忘
项目中oracle存储过程记录--经常使用语法备忘 项目中须要写一个oracle存储过程,需求是收集一个复杂查询的内容(涉及到多张表),然后把符合条件的记录插入到目标表中.当中原表之中的一个的日期字段 ...
- bbs项目中的零碎点记录
一.切换django的语言 在settings中修改django默认的语言 # LANGUAGE_CODE = 'en-us' # 切换django的语言,默认是英语的,我们把他修改为中文 LANGU ...
- 学习用Node.js和Elasticsearch构建搜索引擎(6):实际项目中常用命令使用记录
1.检测集群是否健康. curl -XGET 'localhost:9200/_cat/health?v' #后面加一个v表示让输出内容表格显示表头 绿色表示一切正常,黄色表示所有的数据可用但是部分副 ...
- vue及webpack在项目中的一些优化
传送:https://www.haorooms.com/post/vue_webpack_youhua
- Spring-Gateway与Spring-Security在前后端分离项目中的实践
前言 网上貌似webflux这一套的SpringSecurity操作资料貌似很少. 自己研究了一波,记录下来做一点备忘,如果能帮到也在迷惑的人一点点,就更好了. 新项目是前后端分离的项目,前台vue, ...
- oracle性能优化(项目中的一个sql优化的简单记录)
在项目中,写的sql主要以查询为主,但是数据量一大,就会突出sql性能优化的重要性.其实在数据量2000W以内,可以考虑索引,但超过2000W了,就要考虑分库分表这些了.本文主要记录在实际项目中,一个 ...
- WebGIS项目中利用mysql控制点库进行千万条数据坐标转换时的分表分区优化方案
文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/ 1. 背景 项目中有1000万条历史案卷,为某地方坐标系数据,我们的真实 ...
- 记一次ASP.NET MVC性能优化(实际项目中)
前言 在开发中为了紧赶项目进度而未去关注性能的问题,在项目逐渐稳定下来后发现性能令人感到有点忧伤,于是开始去关注这方面,本篇为记录在开发中遇到的问题并解决,不喜勿喷.注意:以下问题都是在移动端上出现, ...
- 创建并在项目中调用SQLSERVER存储过程的简单示例
使用SQLSERVER存储过程可以很大的提高程序运行速度,简化编程维护难度,现已得到广泛应用.创建存储过程 和数据表一样,在使用之前需要创建存储过程,它的简明语法是: 引用: Create PROC ...
随机推荐
- 如何找到java对应的c/c++源码
很多时候java经常被c鄙视,因为c说我是你的基础,但java竟然有如此强的生命力就必然有其存在的价值.本文不探讨各种开发语言的优劣,仅仅介绍如何找到java对应c/c++实现的源码.当我们追究一个j ...
- C语言中extern关键字的使用
C语言中extern关键字的使用,直接上代码. file1.c文件 #include<stdio.h> extern long power(int); int A = 2; int mai ...
- Xcode7 真机免证书调试Cocos2D游戏
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 经过一番实验,现在终于可以在Xcode7上免证书真机调试了: ...
- java wait和notify及 synchronized sleep 总结
java 中线程我一直弄不清线程锁等 所以写了一些例子验证看法: 在这之前先看下API中wait中的解释: wait:方法来之java.lang.Objetc 方法翻译:在其他线程调用此对象的 not ...
- MyBatis进阶(一)运行原理
初次学习MyBatis,自己花了不少时间,理解一件事物是需要时间的.经过多次反复的理解,你的认知能力就可以得到提升.以下是学习MyBatis的一些理解认识,技术理解上若有不当之处,敬请朋友们提出宝贵意 ...
- Zeroc Ice原理介绍
Ice介绍 Ice(Internet Communications Engine)是ZeroC公司的杰作,继承了CORBA的血统,是新一代的面向对象的分布式系统中间件.Ice是RPC通 ...
- 《java入门第一季》之UDP协议下的网络编程小案例
需求,一台电脑发送数据,其他电脑都可以收到该数据.使用广播地址. 发送端: import java.io.BufferedReader; import java.io.IOException; imp ...
- ROS_Kinetic_11 ROS程序基础Eclipse_C++(二)
ROS_Kinetic_11 ROS程序基础Eclipse_C++(二) 编写简单的Service和Client (C++): http://wiki.ros.org/cn/ROS/Tutorials ...
- Linux的启动流程(一)
· 启动第一步--加载BIOS 当你打开计算机电源,计算机会首先加载BIOS信息,BIOS信息是如此的重要,以至于计算机必须在最开始就找到它.这是因为BIOS中包含了CPU的相关信息.设备启动顺序信息 ...
- C++ Primer 有感(类)
1.在类内部,声明成员函数时必需 的,而定义成员函数则是可选的.在类内部定义的函数默认为inline. 2.const成员函数不能改变其所操作的对象的数据成员.const必须同时出现在声明和定义中,若 ...