oracle 10g 的max 的bug
发现一个简单的函数,返回不是预期的结果
create or replace function get_num(CONS_ID in number) return varchar2
is
v_cons_no varchar2(20);
begin
select max(cons_no) into v_cons_no from c_cons where cons_id = CONS_ID;
return v_cons_no;
end get_num;
/
其中cons_id为number(16) 是表c_cons的主键
cons_no为varchar2(16)
执行 select sgpm.get_num(31334506) from dual ;
当cons_id = 31334506 时,表中不存在记录, 但是返回的结果却是c_cons中最大的cons_no
所以很奇怪。
进行跟踪 ,查看执行过程
SQL> alter session set events '10046 trace name context forever ,level 12' ;
Session altered
SQL> select sgpm.get_num(31334506) from dual ;
SGPM.GET_NUM(31334506)
--------------------------------------------------------------------------------
9990000707
SQL> alter session set events '10046 trace name context off' ;
Session altered
trace文件如下
=====================
PARSING IN CURSOR #2 len=42 dep=0 uid=0 oct=3 lid=0 tim=1235027784847616 hv=372642674 ad='27f1b580'
select sgpm.get_num(31334506) from dual
END OF STMT
PARSE #2:c=2999,e=51732,p=0,cr=0,cu=0,mis=1,r=0,dep=0,og=1,tim=1235027784847613
BINDS #2:
EXEC #2:c=0,e=31,p=0,cr=0,cu=0,mis=0,r=0,dep=0,og=1,tim=1235027784847690
WAIT #2: nam='SQL*Net message to client' ela= 1 driver id=1413697536 #bytes=1 p3=0 obj#=-1 tim=1235027784847711
WAIT #2: nam='SQL*Net message from client' ela= 924 driver id=1413697536 #bytes=1 p3=0 obj#=-1 tim=123502778484867
1
=====================
PARSING IN CURSOR #1 len=130 dep=1 uid=0 oct=3 lid=0 tim=1235027784859024 hv=2920152077 ad='5dafd7b0'
select procedure#,procedurename,properties,itypeobj# from procedureinfo$ where obj#=:1 order by procedurename desc
, overload# desc
END OF STMT
PARSE #1:c=1000,e=10193,p=0,cr=0,cu=0,mis=1,r=0,dep=1,og=4,tim=1235027784859021
BINDS #1:
kkscoacd
Bind#0
oacdty=02 mxl=22(22) mxlc=00 mal=00 scl=00 pre=00
oacflg=00 fl2=0001 frm=00 csi=00 siz=24 off=0
kxsbbbfp=2a9724c740 bln=22 avl=04 flg=05
value=334008
EXEC #1:c=1000,e=18718,p=0,cr=0,cu=0,mis=1,r=0,dep=1,og=4,tim=1235027784877798
FETCH #1:c=0,e=187,p=0,cr=3,cu=0,mis=0,r=1,dep=1,og=4,tim=1235027784878008
=====================
PARSING IN CURSOR #3 len=273 dep=1 uid=0 oct=3 lid=0 tim=1235027784878500 hv=3080304803 ad='30dba868'
select position#,sequence#,level#,argument,type#,charsetid,charsetform,properties,nvl(length, 0), nvl(precision#,
0),nvl(scale, 0),nvl(radix, 0), type_owner,type_name,type_subname,type_linkname,pls_type from argument$ where obj#
=:1 and procedure#=:2 order by sequence# desc
END OF STMT
PARSE #3:c=0,e=442,p=0,cr=0,cu=0,mis=1,r=0,dep=1,og=4,tim=1235027784878498
BINDS #3:
kkscoacd
Bind#0
oacdty=02 mxl=22(22) mxlc=00 mal=00 scl=00 pre=00
oacflg=00 fl2=0001 frm=00 csi=00 siz=48 off=0
kxsbbbfp=2a972478e8 bln=22 avl=04 flg=05
value=334008
Bind#1
oacdty=02 mxl=22(22) mxlc=00 mal=00 scl=00 pre=00
oacflg=00 fl2=0001 frm=00 csi=00 siz=0 off=24
kxsbbbfp=2a97247900 bln=22 avl=02 flg=01
value=1
EXEC #3:c=1000,e=55553,p=0,cr=0,cu=0,mis=1,r=0,dep=1,og=4,tim=1235027784934119
FETCH #3:c=999,e=195,p=0,cr=3,cu=0,mis=0,r=1,dep=1,og=4,tim=1235027784934333
FETCH #3:c=0,e=20,p=0,cr=2,cu=0,mis=0,r=1,dep=1,og=4,tim=1235027784934392
FETCH #3:c=0,e=7,p=0,cr=1,cu=0,mis=0,r=0,dep=1,og=4,tim=1235027784934424
FETCH #1:c=0,e=11,p=0,cr=1,cu=0,mis=0,r=0,dep=1,og=4,tim=1235027784934468
STAT #1 id=1 cnt=1 pid=0 pos=1 obj=70 op='TABLE ACCESS BY INDEX ROWID PROCEDUREINFO$ (cr=4 pr=0 pw=0 time=191 us)'
STAT #1 id=2 cnt=1 pid=1 pos=1 obj=110 op='INDEX RANGE SCAN DESCENDING I_PROCEDUREINFO1 (cr=3 pr=0 pw=0 time=159 u
s)'
STAT #3 id=1 cnt=2 pid=0 pos=1 obj=71 op='TABLE ACCESS BY INDEX ROWID ARGUMENT$ (cr=6 pr=0 pw=0 time=189 us)'
STAT #3 id=2 cnt=2 pid=1 pos=1 obj=112 op='INDEX RANGE SCAN DESCENDING I_ARGUMENT2 (cr=4 pr=0 pw=0 time=177 us)'
=====================
PARSING IN CURSOR #1 len=53 dep=1 uid=0 oct=3 lid=0 tim=1235027784940236 hv=1552457782 ad='5d3b4208'
select max(procedure#) from procedurec$ where obj#=:1
END OF STMT
PARSE #1:c=1000,e=5635,p=0,cr=0,cu=0,mis=1,r=0,dep=1,og=4,tim=1235027784940233
BINDS #1:
kkscoacd
Bind#0
oacdty=02 mxl=22(22) mxlc=00 mal=00 scl=00 pre=00
oacflg=00 fl2=0001 frm=00 csi=00 siz=24 off=0
kxsbbbfp=2a9724ebb8 bln=22 avl=04 flg=05
value=334008
EXEC #1:c=1000,e=14920,p=0,cr=0,cu=0,mis=1,r=0,dep=1,og=4,tim=1235027784955199
FETCH #1:c=0,e=51,p=0,cr=2,cu=0,mis=0,r=1,dep=1,og=4,tim=1235027784955263
STAT #1 id=1 cnt=1 pid=0 pos=1 obj=0 op='SORT AGGREGATE (cr=2 pr=0 pw=0 time=72 us)'
STAT #1 id=2 cnt=0 pid=1 pos=1 obj=0 op='FIRST ROW (cr=2 pr=0 pw=0 time=45 us)'
STAT #1 id=3 cnt=0 pid=2 pos=1 obj=329 op='INDEX RANGE SCAN (MIN/MAX) I_PROCEDUREC$ (cr=2 pr=0 pw=0 time=43 us)'
=====================
PARSING IN CURSOR #1 len=57 dep=1 uid=0 oct=3 lid=0 tim=1235027784974912 hv=2515040609 ad='3722edf0'
select max(procedure#) from procedureplsql$ where obj#=:1
END OF STMT
PARSE #1:c=0,e=19549,p=0,cr=0,cu=0,mis=1,r=0,dep=1,og=4,tim=1235027784974902
BINDS #1:
kkscoacd
Bind#0
oacdty=02 mxl=22(22) mxlc=00 mal=00 scl=00 pre=00
oacflg=00 fl2=0001 frm=00 csi=00 siz=24 off=0
kxsbbbfp=2a9724ebb8 bln=22 avl=04 flg=05
value=334008
EXEC #1:c=3000,e=10180,p=0,cr=0,cu=0,mis=1,r=0,dep=1,og=4,tim=1235027784985181
FETCH #1:c=0,e=127,p=0,cr=2,cu=0,mis=0,r=1,dep=1,og=4,tim=1235027784985324
STAT #1 id=1 cnt=1 pid=0 pos=1 obj=0 op='SORT AGGREGATE (cr=2 pr=0 pw=0 time=137 us)'
STAT #1 id=2 cnt=1 pid=1 pos=1 obj=0 op='FIRST ROW (cr=2 pr=0 pw=0 time=117 us)'
STAT #1 id=3 cnt=1 pid=2 pos=1 obj=331 op='INDEX RANGE SCAN (MIN/MAX) I_PROCEDUREPLSQL$ (cr=2 pr=0 pw=0 time=115 u
s)'
=====================
PARSING IN CURSOR #1 len=56 dep=1 uid=0 oct=3 lid=0 tim=1235027784999944 hv=4110536835 ad='5e578ea0'
select max(procedure#) from procedurejava$ where obj#=:1
END OF STMT
PARSE #1:c=0,e=14510,p=0,cr=0,cu=0,mis=1,r=0,dep=1,og=4,tim=1235027784999942
BINDS #1:
kkscoacd
Bind#0
oacdty=02 mxl=22(22) mxlc=00 mal=00 scl=00 pre=00
oacflg=00 fl2=0001 frm=00 csi=00 siz=24 off=0
kxsbbbfp=2a9724ebb8 bln=22 avl=04 flg=05
value=334008
EXEC #1:c=0,e=3679,p=0,cr=0,cu=0,mis=1,r=0,dep=1,og=4,tim=1235027785003660
FETCH #1:c=1000,e=64,p=0,cr=2,cu=0,mis=0,r=1,dep=1,og=4,tim=1235027785003736
STAT #1 id=1 cnt=1 pid=0 pos=1 obj=0 op='SORT AGGREGATE (cr=2 pr=0 pw=0 time=69 us)'
STAT #1 id=2 cnt=0 pid=1 pos=1 obj=0 op='FIRST ROW (cr=2 pr=0 pw=0 time=62 us)'
STAT #1 id=3 cnt=0 pid=2 pos=1 obj=327 op='INDEX RANGE SCAN (MIN/MAX) I_PROCEDUREJAVA$ (cr=2 pr=0 pw=0 time=62 us)
'
=====================
PARSING IN CURSOR #1 len=80 dep=1 uid=0 oct=3 lid=0 tim=1235027785004767 hv=861121043 ad='49c15a68'
select procedure#,entrypoint# from procedurec$ where obj#=:1 order by procedure#
END OF STMT
PARSE #1:c=999,e=951,p=0,cr=0,cu=0,mis=1,r=0,dep=1,og=4,tim=1235027785004764
BINDS #1:
kkscoacd
Bind#0
oacdty=02 mxl=22(22) mxlc=00 mal=00 scl=00 pre=00
oacflg=00 fl2=0001 frm=00 csi=00 siz=24 off=0
kxsbbbfp=2a9724c740 bln=22 avl=04 flg=05
value=334008
EXEC #1:c=0,e=585,p=0,cr=0,cu=0,mis=1,r=0,dep=1,og=4,tim=1235027785005388
FETCH #1:c=0,e=14,p=0,cr=2,cu=0,mis=0,r=0,dep=1,og=4,tim=1235027785005415
STAT #1 id=1 cnt=0 pid=0 pos=1 obj=328 op='TABLE ACCESS BY INDEX ROWID PROCEDUREC$ (cr=2 pr=0 pw=0 time=17 us)'
STAT #1 id=2 cnt=0 pid=1 pos=1 obj=329 op='INDEX RANGE SCAN I_PROCEDUREC$ (cr=2 pr=0 pw=0 time=14 us)'
=====================
PARSING IN CURSOR #1 len=84 dep=1 uid=0 oct=3 lid=0 tim=1235027785026999 hv=2708662576 ad='4966ee00'
select procedure#,entrypoint# from procedureplsql$ where obj#=:1 order by procedure#
END OF STMT
PARSE #1:c=0,e=21521,p=0,cr=0,cu=0,mis=1,r=0,dep=1,og=4,tim=1235027785026997
BINDS #1:
kkscoacd
Bind#0
oacdty=02 mxl=22(22) mxlc=00 mal=00 scl=00 pre=00
oacflg=00 fl2=0001 frm=00 csi=00 siz=24 off=0
kxsbbbfp=2a9724c740 bln=22 avl=04 flg=05
value=334008
EXEC #1:c=1000,e=513,p=0,cr=0,cu=0,mis=1,r=0,dep=1,og=4,tim=1235027785027548
FETCH #1:c=0,e=57,p=0,cr=3,cu=0,mis=0,r=1,dep=1,og=4,tim=1235027785027618
FETCH #1:c=0,e=2,p=0,cr=0,cu=0,mis=0,r=0,dep=1,og=4,tim=1235027785027644
STAT #1 id=1 cnt=1 pid=0 pos=1 obj=330 op='TABLE ACCESS BY INDEX ROWID PROCEDUREPLSQL$ (cr=3 pr=0 pw=0 time=58 us)
'
STAT #1 id=2 cnt=1 pid=1 pos=1 obj=331 op='INDEX RANGE SCAN I_PROCEDUREPLSQL$ (cr=2 pr=0 pw=0 time=11 us)'
=====================
PARSING IN CURSOR #1 len=142 dep=1 uid=0 oct=3 lid=0 tim=1235027785040149 hv=224838358 ad='496dc350'
select procedure#,ownerlength,classlength,methodlength,siglength, flagslength,cookiesize from procedurejava$ where
obj#=:1 order by procedure#
END OF STMT
PARSE #1:c=0,e=12444,p=0,cr=0,cu=0,mis=1,r=0,dep=1,og=4,tim=1235027785040147
BINDS #1:
kkscoacd
Bind#0
oacdty=02 mxl=22(22) mxlc=00 mal=00 scl=00 pre=00
oacflg=00 fl2=0001 frm=00 csi=00 siz=24 off=0
kxsbbbfp=2a9724c740 bln=22 avl=04 flg=05
value=334008
EXEC #1:c=0,e=14247,p=0,cr=0,cu=0,mis=1,r=0,dep=1,og=4,tim=1235027785054434
FETCH #1:c=0,e=12,p=0,cr=2,cu=0,mis=0,r=0,dep=1,og=4,tim=1235027785054458
STAT #1 id=1 cnt=0 pid=0 pos=1 obj=326 op='TABLE ACCESS BY INDEX ROWID PROCEDUREJAVA$ (cr=2 pr=0 pw=0 time=14 us)'
STAT #1 id=2 cnt=0 pid=1 pos=1 obj=327 op='INDEX RANGE SCAN I_PROCEDUREJAVA$ (cr=2 pr=0 pw=0 time=12 us)'
=====================
PARSING IN CURSOR #3 len=125 dep=1 uid=0 oct=3 lid=0 tim=1235027785054579 hv=2747448943 ad='49c46030'
select ownername,classname,methodname,signature,flags from procedurejava$ where obj#=:1 and procedure#=:2 order by
procedure#
END OF STMT
PARSE #3:c=0,e=50,p=0,cr=0,cu=0,mis=0,r=0,dep=1,og=4,tim=1235027785054577
=====================
PARSING IN CURSOR #1 len=55 dep=1 uid=62 oct=3 lid=62 tim=1235027785280498 hv=1246319932 ad='40667808'
SELECT MAX(CONS_NO) FROM C_CONS WHERE CONS_ID =CONS_ID
END OF STMT
PARSE #1:c=0,e=225745,p=0,cr=0,cu=0,mis=1,r=0,dep=1,og=1,tim=1235027785280495
BINDS #1:
EXEC #1:c=0,e=37,p=0,cr=0,cu=0,mis=0,r=0,dep=1,og=1,tim=1235027785280575
WAIT #1: nam='db file scattered read' ela= 11860 file#=15 block#=321036 blocks=5 obj#=167877 tim=1235027785337169
WAIT #1: nam='db file scattered read' ela= 79 file#=16 block#=742249 blocks=8 obj#=167877 tim=1235027785339348
.
.
.
.
.
WAIT #1: nam='db file scattered read' ela= 7283 file#=15 block#=1150219 blocks=126 obj#=167968 tim=123502787132504
3
WAIT #1: nam='db file scattered read' ela= 743 file#=16 block#=1128587 blocks=2 obj#=167968 tim=1235027871327030
WAIT #1: nam='db file sequential read' ela= 32 file#=15 block#=321771 blocks=1 obj#=167969 tim=1235027871327127
WAIT #1: nam='db file scattered read' ela= 481 file#=15 block#=321772 blocks=5 obj#=167969 tim=1235027871327688
FETCH #1:c=15222686,e=86047167,p=594718,cr=611627,cu=0,mis=0,r=1,dep=1,og=1,tim=1235027871327760
WAIT #2: nam='SQL*Net message to client' ela= 1 driver id=1413697536 #bytes=1 p3=0 obj#=167969 tim=123502787132787
6
FETCH #2:c=15237684,e=86479198,p=594718,cr=611650,cu=0,mis=0,r=1,dep=0,og=1,tim=1235027871327900
WAIT #2: nam='SQL*Net message from client' ela= 4257 driver id=1413697536 #bytes=1 p3=0 obj#=167969 tim=1235027871
332234
STAT #2 id=1 cnt=1 pid=0 pos=1 obj=0 op='FAST DUAL (cr=0 pr=0 pw=0 time=3 us)'
=====================
PARSING IN CURSOR #3 len=61 dep=0 uid=0 oct=47 lid=0 tim=1235027871332375 hv=356401299 ad='5ed1f480'
begin :id := sys.dbms_transaction.local_transaction_id; end;
END OF STMT
PARSE #3:c=0,e=35,p=0,cr=0,cu=0,mis=0,r=0,dep=0,og=1,tim=1235027871332374
BINDS #3:
kkscoacd
Bind#0
oacdty=01 mxl=4000(3999) mxlc=00 mal=00 scl=00 pre=00
oacflg=01 fl2=1000000 frm=01 csi=871 siz=4000 off=0
kxsbbbfp=2a9724f060 bln=4000 avl=00 flg=05
WAIT #3: nam='SQL*Net message to client' ela= 1 driver id=1413697536 #bytes=1 p3=0 obj#=167969 tim=123502787133254
9
EXEC #3:c=0,e=138,p=0,cr=0,cu=0,mis=0,r=1,dep=0,og=1,tim=1235027871332568
*** 2010-01-28 16:50:13.702
WAIT #3: nam='SQL*Net message from client' ela= 71736013 driver id=1413697536 #bytes=1 p3=0 obj#=167969 tim=123502
7943068617
=====================
PARSING IN CURSOR #2 len=21 dep=0 uid=0 oct=3 lid=0 tim=1235027943068916 hv=2564406738 ad='5ed1ba40'
select 'x' from dual
END OF STMT
PARSE #2:c=1000,e=89,p=0,cr=0,cu=0,mis=0,r=0,dep=0,og=1,tim=1235027943068913
BINDS #2:
EXEC #2:c=0,e=42,p=0,cr=0,cu=0,mis=0,r=0,dep=0,og=1,tim=1235027943068999
WAIT #2: nam='SQL*Net message to client' ela= 2 driver id=1413697536 #bytes=1 p3=0 obj#=167969 tim=123502794306902
3
WAIT #2: nam='SQL*Net message from client' ela= 627 driver id=1413697536 #bytes=1 p3=0 obj#=167969 tim=12350279430
69689
WAIT #2: nam='SQL*Net message to client' ela= 1 driver id=1413697536 #bytes=1 p3=0 obj#=167969 tim=123502794306971
9
FETCH #2:c=0,e=25,p=0,cr=0,cu=0,mis=0,r=1,dep=0,og=1,tim=1235027943069733
WAIT #2: nam='SQL*Net message from client' ela= 8905 driver id=1413697536 #bytes=1 p3=0 obj#=167969 tim=1235027943
078671
STAT #2 id=1 cnt=1 pid=0 pos=1 obj=0 op='FAST DUAL (cr=0 pr=0 pw=0 time=3 us)'
=====================
PARSING IN CURSOR #3 len=61 dep=0 uid=0 oct=47 lid=0 tim=1235027943078752 hv=356401299 ad='5ed1f480'
begin :id := sys.dbms_transaction.local_transaction_id; end;
END OF STMT
PARSE #3:c=0,e=15,p=0,cr=0,cu=0,mis=0,r=0,dep=0,og=1,tim=1235027943078750
BINDS #3:
kkscoacd
Bind#0
oacdty=01 mxl=4000(3999) mxlc=00 mal=00 scl=00 pre=00
oacflg=01 fl2=1000000 frm=01 csi=871 siz=4000 off=0
kxsbbbfp=2a9724f060 bln=4000 avl=00 flg=05
WAIT #3: nam='SQL*Net message to client' ela= 1 driver id=1413697536 #bytes=1 p3=0 obj#=167969 tim=123502794307890
3
EXEC #3:c=0,e=126,p=0,cr=0,cu=0,mis=0,r=1,dep=0,og=1,tim=1235027943078919
WAIT #3: nam='SQL*Net message from client' ela= 698 driver id=1413697536 #bytes=1 p3=0 obj#=167969 tim=12350279430
79645
=====================
PARSING IN CURSOR #2 len=58 dep=0 uid=0 oct=42 lid=0 tim=1235027943098745 hv=561549715 ad='0'
alter session set events '10046 trace name context off'
END OF STMT
PARSE #2:c=0,e=19055,p=0,cr=0,cu=0,mis=0,r=0,dep=0,og=0,tim=1235027943098736
EXEC #2:c=0,e=36,p=0,cr=0,cu=0,mis=0,r=0,dep=0,og=0,tim=1235027943098857
这个似乎是Oracle的bug,如果数据库没有打patch的话,只要将查询稍微变形一下,就可以避免这个错误
execute immediate 'select max(cons_no)
from c_cons where cons_id ='||CONS_ID into v_cons_no ;