一起ORACLE数据库中数据查询结果不一致问题的排查过程
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了一起ORACLE数据库中数据查询结果不一致问题的排查过程,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5183字,纯文字阅读大概需要8分钟。
内容图文
![一起ORACLE数据库中数据查询结果不一致问题的排查过程](/upload/InfoBanner/zyjiaocheng/449/d31ac6f5bfa44fba972f93dff9787b7e.jpg)
插入数据的存储过程语句如下:
begin
declare v_i int;v_procname varchar(50);v_employeeinfotbl varchar(50);strsql varchar(4000);
begin
v_i := 0;
while v_i < 10 loop
v_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
)
as
v_employeecnt int;
begin
v_retcode := 0;
select count(*) into v_employeecnt from ‘||v_employeeinfotbl||‘ where employeeno = v_employeeno;
if v_employeecnt > 0 then -- the employeeno is already in DB
begin
v_retcode := 1;
return;
end;
else -- the employeeno is not in DB
begin
insert into ‘||v_employeeinfotbl||‘(employeeno, employeeage) values(v_employeeno, v_employeeage);
end;
end if;
exception when others then
begin
rollback;
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;”语句惹的祸吗?
三、问题原因
我们将存储过程的代码修改为如下:
begin
declare v_i int;v_procname varchar(50);v_employeeinfotbl varchar(50);strsql varchar(4000);
begin
v_i := 0;
while v_i < 10 loop
v_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
)
as
v_employeecnt int;
begin
v_retcode := 0;
select count(*) into v_employeecnt from ‘||v_employeeinfotbl||‘ where employeeno = v_employeeno;
if v_employeecnt > 0 then -- the employeeno is already in DB
begin
v_retcode := 1;
return;
end;
else -- the employeeno is not in DB
begin
insert into ‘||v_employeeinfotbl||‘(employeeno, employeeage) values(v_employeeno, v_employeeage);
commit;
end;
end if;
exception when others then
begin
rollback;
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数据库中数据查询结果不一致问题的排查过程
标签:
本文系统来源:http://blog.csdn.net/zhouzhaoxiong1227/article/details/46377225
内容总结
以上是互联网集市为您收集整理的一起ORACLE数据库中数据查询结果不一致问题的排查过程全部内容,希望文章能够帮你解决一起ORACLE数据库中数据查询结果不一致问题的排查过程所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。