一起ORACLE数据库中数据查询结果不一致问题的排查过程
一、问题描述
在某软件开发项目中,需要在ORACLE数据库中建立十张类型相同的员工信息表tb_employeeinfo0~tb_employeeinfo9,并建立向这十张表中插入数据的存储过程。ORACLE数据库安装在Linux操作系统下。
为了操作上的方便性,开发人员在PL/SQL Developer软件(ORACLE数据库开发软件)上实现了建表和建存储过程的操作。之后,开发人员利用SQL语句在在PL/SQL Developer软件上实现了向某个数据表中插入数据的操作。利用select语句查询到数据被成功插入到数据库中。
一段时间之后,该开发人员在Linux下以命令行方式登录到数据库中,并利用select语句从员工信息表中查询数据,发现数据条数为0。“难道是数据被删除了?”该开发人员一头雾水。他在PL/SQL Developer软件上利用select语句从员工信息表中查询数据,发现数据是存在的。
到底是哪里出了问题呢?
二、问题排查
我们在开发小组的自测环境上还原了问题出现的整个过程。下面让我们一步一步来看。
员工信息表的建表语句如下:
-- tb_employeeinfo0~9
begindeclare i int;tmpcount int;tbname varchar2(50);strsql varchar2(1000);begini:=0;while i<10 loopbegintbname := 'tb_employeeinfo'||to_char(i);i := i+1;select count(1) into tmpcount from user_tables where table_name = Upper(tbname);if tmpcount>0 thenbeginexecute immediate 'drop table '||tbname;commit;end;end if;strsql := 'create table '||tbname||'(employeeno varchar2(10) not null, -- employee numberemployeeage int not null -- employee age)';execute immediate strsql; strsql := 'begin execute immediate ''drop index idx1_'||tbname || ' '''|| ';exception when others then null;end;';execute immediate strsql;execute immediate 'create unique index idx1_'||tbname||' on '||tbname||'(employeeno)';end;end loop;end;
end;
/
插入数据的存储过程语句如下:
begindeclare v_i int;v_procname varchar(50);v_employeeinfotbl varchar(50);strsql varchar(4000);
beginv_i := 0;while v_i < 10 loopv_procname := 'pr_insertdata'||substr(to_char(v_i),1,1);v_employeeinfotbl := 'tb_employeeinfo'||substr(to_char(v_i),1,1);v_i := v_i + 1;strsql := 'create or replace procedure '||v_procname||'(v_employeeno in varchar2,v_employeeage in int,v_retcode out int -- 0_success, 1,2_fail)asv_employeecnt int;beginv_retcode := 0;select count(*) into v_employeecnt from '||v_employeeinfotbl||' where employeeno = v_employeeno;if v_employeecnt > 0 then -- the employeeno is already in DBbeginv_retcode := 1;return;end;else -- the employeeno is not in DBbegininsert into '||v_employeeinfotbl||'(employeeno, employeeage) values(v_employeeno, v_employeeage);end;end if;exception when others thenbeginrollback;v_retcode := 2; return;end;end;';execute immediate strsql;end loop;end;
end;
/
我们在PL/SQL Developer软件上执行了以上SQL语句(注意:先建表,后建存储过程)之后,利用以下SQL语句向tb_employeeinfo6表中插入数据:
set serveroutput on
declare v_retcode int;
begin
pr_insertdata6('123456', 25, v_retcode);
dbms_output.put_line( v_retcode);
end;
/
执行“select * from tb_employeeinfo6;”语句查询数据,结果如下:
SQL> select * from tb_employeeinfo6;
EMPLOYEENO EMPLOYEEAGE
---------------------------
123456 25
可见,数据插入成功。
接着,我们利用以下命令行从Linux系统上登录到ORACLE数据库中(注意:username是指数据库用户名,password是指数据库密码,databaseservername是指数据库服务名):
sqlplus /nolog
connect username/password@databaseservername
然后执行如下查询语句:
select * from tb_employeeinfo6;
发现返回的值为空,即该数据表中没有数据。
真是奇怪了,为什么同样的查询语句,两边的执行结果不一致呢?
我们回过头来详细阅读了建表和建存储过程的代码,没看出有明显的问题。我们将该问题告诉了一位工作多年的老员工,请他来帮我们分析问题的原因所在。他详细看了我们的SQL语句之后,便指出存储过程的代码有点问题,在向表中插入数据之后忘记提交了。也就是说,存储过程中的“insert…”语句之后应该加上“commit;”。
难道就是这个“commit;”语句惹的祸吗?
三、问题原因
我们将存储过程的代码修改为如下:
begindeclare v_i int;v_procname varchar(50);v_employeeinfotbl varchar(50);strsql varchar(4000);
beginv_i := 0;while v_i < 10 loopv_procname := 'pr_insertdata'||substr(to_char(v_i),1,1);v_employeeinfotbl := 'tb_employeeinfo'||substr(to_char(v_i),1,1);v_i := v_i + 1;strsql := 'create or replace procedure '||v_procname||'(v_employeeno in varchar2,v_employeeage in int,v_retcode out int -- 0_success, 1,2_fail)asv_employeecnt int;beginv_retcode := 0;select count(*) into v_employeecnt from '||v_employeeinfotbl||' where employeeno = v_employeeno;if v_employeecnt > 0 then -- the employeeno is already in DBbeginv_retcode := 1;return;end;else -- the employeeno is not in DBbegininsert into '||v_employeeinfotbl||'(employeeno, employeeage) values(v_employeeno, v_employeeage);commit;end;end if;exception when others thenbeginrollback;v_retcode := 2; return;end;end;';execute immediate strsql;end loop;end;
end;
/
接着,我们在PL/SQL Developer软件上执行了以上SQL语句,并利用以下SQL语句向tb_employeeinfo9表中插入数据:
set serveroutput on
declare v_retcode int;
begin
pr_insertdata9('123469', 25, v_retcode);
dbms_output.put_line( v_retcode);
end;
/
同样在该软件上执行“select * from tb_ employeeinfo9;”语句查询数据,结果如下:
SQL> select * from tb_employeeinfo9;
EMPLOYEENO EMPLOYEEAGE
--------------------------
123469 25
然后在Linux系统上执行“select * from tb_employeeinfo9;”语句,结果如下:
SQL> select * from tb_employeeinfo9;
EMPLOYEENO EMPLOYEEAGE
------------------------
123469 25
可见,数据被成功插入到员工信息表中。
四、总结
对于本次因为“commit;”而引发的问题,我们的总结如下:
第一,在动手编写代码之前,一定要对语法规则了然于心,不要让一个小小的问题引起整个软件功能的异常。
第二,在软件开发中,经验十分的重要。一个新人花几个小时不能解决的问题,一个老手可能几分钟就搞定了。因此,在遇到自己不能解决的问题的时候,我们一定要勤于开口,多多向有经验的老员工请教。
本人微信公众号:zhouzxi,请扫描以下二维码:
一起ORACLE数据库中数据查询结果不一致问题的排查过程相关推荐
- navicat导出数据到oracle,使用Navicat premium导出oracle数据库中数据到SQL server2008数据库中...
使用Navicat premium导出oracle数据库中数据到SQL server2008数据库中 发布时间:2018-08-20 14:41, 浏览次数:471 , 标签: Navicat pre ...
- oracle中的表怎样保存,怎样保存excel 表格数据库中-如何将excel表格更新oracle数据库中数据...
如何将excel表格更新oracle数据库中数据 这里以SQL SERVE2008为例.SQLSERVER2008有一个"数据导入导出功能",当然我们也可以打开数据库之后,在数据库 ...
- oracle+资料类型不一致吗,oracle数据库中,字段类型不一致,导致查询慢
最近一个WEBSERVICE突然变慢了,后查询发现,后台查询也非常慢(记录条数800多万),索引也有,如下语句 SELECT P.ID,P.RECORD_ID,P.KEY_NAME,P.KEY_CON ...
- php数据库中数据查询
MySQL查询语句 mysql_fetch_row,mysql_fetch_array,mysql_fetch_object,mysql_fetch_assoc 区别用法: mysql_fetch_r ...
- oracle数据库分页查询慢,Oracle数据库中分页查询中排序及效率问题
原始未分页查询Sql代码如下: select ROWNUM rn, t.id ID, o.name YYB,u.name XM, t.MC from tZDYSX t,tuser u,lborgani ...
- oracle 数据库中数据导出到excel
确保安装了PLSQL Developer工具,连接数据库. FIle--new--SQL window 运行查询,选中要导出的数据,右键--copy to excel. 或者 运行查询后,右键--se ...
- Oracle数据库中有关记录个数的查询
一.查询表中全部的记录个数 可用两种方法,一种是在oracle的系统表中统计,另一种需要写存储过程统计,方法分别如下. 1.系统表中统计: SELECT sum(num_rows) FROM user ...
- 查询oracle数据库的表格数据类型,excel表格中如何查询数据库数据类型-我想把excel表格中的数据导入oracle数据库中,想在......
在excel表里,什么是:字段.记录.数据类型.多工... declare @t table(id numeric(18,2)) insert into @t SELECT col1 FROM ...
- oracle语句mysql数据库名称_查询oracle数据库中当前数据库所有表的名称
SQL查询数据库中所有指定类型的字段名称和所在的表名 --查询数据库中所有指定类型的字段名称和所在的表名 --eg: 下面查的是当前数据库中 所有字段类型为 nvarchar(max) 的字段名和表名 ...
最新文章
- (转)如何禁用Windows 10系统的触摸屏
- 国家计算机二级申诉,CCF关于CSP-J/S2020第二轮认证申诉的通知
- 最新|TensorFlow开源的序列到序列框架
- BZOJ2209: [Jsoi2011]括号序列
- 我滴个乖乖,我复现了Spring的漏洞,害怕!
- es max virtual memory areas vm.max_map_count [65530] likely too low, increase to at least [262144]
- php 特有,PHP特殊数据类型有哪些?原来这门编程语言如此有趣
- Java中获取资源文件路径
- python编辑器是什么_python开发用什么编辑器
- java中如何将string 转化成long
- cloudflare免费设置_Cloudflare 入门教程:使用 Cloudflare 免费 CDN 加速 amp; 保护自己的网站...
- ARMv7的OP-TEE源代码的获取和编译
- Raki的网络流24题题解总结
- Add Juniper SRX Cluster into JunOS Space 16.1 Security Director
- gps84转换gcj02公式_WGS84-GCJ-02坐标转化
- GNN学习笔记(三) Graph Neural Network概述
- utorrent不能下载的解决方法
- pdf太大怎么压缩大小?
- 电脑键盘上的Alt键的作用
- ❤ CSDN榜一博主,半年文章汇总【答谢粉丝、文末送书4本】❤
热门文章
- Elasticsearch性能优化实战指南
- 李宏毅强化学习完整笔记!开源项目《LeeDeepRL-Notes》发布
- 如何用XGBoost做时间序列预测?
- 树模型集成学习(Tree Embedding)
- 研究生扩招20.74%!教育部公布重要数据
- ICCV 2021 | 国科大提出首个CNN和Transformer双体主干网络!Conformer准确率高达84.1%!...
- 最新视觉Transformer综述(2017-2020年)
- 你也许只使用到了 VS Code 20% 的功能
- 一文看懂 Bahdanau 和 Luong 两种 Attention 机制的区别
- 无需代码即可看视频造游戏!英伟达再现神操作!