我的plsql代码有问题,它跳过where子句,如果它在一个包中,并给我错误的结果.我做了一个小测试,你可以跑.需要在SQL Developer中启用DBMS输出并按顺序运行,一次一个命令/块.Oracle DB是版本12C.
create table tmp_bounding_box (
id number,
x number(14,10),
y number(14,10)
);
insert into tmp_bounding_box (id,x,y) values(1,0.0,0.0);
insert into tmp_bounding_box (id,x,y) values(1,0.0,4.0);
insert into tmp_bounding_box (id,x,y) values(1,4.0,4.0);
insert into tmp_bounding_box (id,x,y) values(1,4.0,0.0);
insert into tmp_bounding_box (id,x,y) values(2,1.0,1.0);
insert into tmp_bounding_box (id,x,y) values(2,1.0,3.0);
insert into tmp_bounding_box (id,x,y) values(2,3.0,3.0);
insert into tmp_bounding_box (id,x,y) values(2,3.0,1.0);
commit;
declare
MAX_X number(14,10) := 0;
MIN_X number(14,10) := 0;
MAX_Y number(14,10) := 0;
MIN_Y number(14,10) := 0;
log_msg varchar2(4000);
begin
select max(x), min(x), max(y), min(y) into MAX_X, MIN_X, MAX_Y, MIN_Y
from tmp_bounding_box where id in ( 2 );
log_msg := 'Bounding box: max_x<'||MAX_X||'> min_x<'||MIN_X||'> max_y<'||MAX_Y||'> min_y<'||MIN_Y||'>';
dbms_output.put_line(log_msg);
end;
create or replace PACKAGE bounding_box_pck as
procedure GetBoundingBox( id number );
end bounding_box_pck;
create or replace PACKAGE BODY bounding_box_pck as
procedure GetBoundingBox( id number ) is
MAX_X number(14,10) := 0;
MIN_X number(14,10) := 0;
MAX_Y number(14,10) := 0;
MIN_Y number(14,10) := 0;
log_msg varchar2(4000);
begin
select max(x), min(x), max(y), min(y) into MAX_X, MIN_X, MAX_Y, MIN_Y
from tmp_bounding_box where id in ( id );
log_msg := 'Bounding box: max_x<'||MAX_X||'> min_x<'||MIN_X||'> max_y<'||MAX_Y||'> min_y<'||MIN_Y||'>';
dbms_output.put_line(log_msg);
end GetBoundingBox;
end bounding_box_pck;
begin
bounding_box_pck.GetBoundingBox(2);
end;
Run Code Online (Sandbox Code Playgroud)
第一个pl/sql块返回:
Bounding box: max_x<3> min_x<1> max_y<3> min_y<1>
Run Code Online (Sandbox Code Playgroud)
第二个pl/sql块返回:
Bounding box: max_x<4> min_x<0> max_y<4> min_y<0>
Run Code Online (Sandbox Code Playgroud)
我一直试图找出为什么会发生这种情况但没有结果.
在你的第二个程序
select max(x), min(x), max(y), min(y) into MAX_X, MIN_X, MAX_Y, MIN_Y
from tmp_bounding_box where id in ( id );
Run Code Online (Sandbox Code Playgroud)
此查询在where子句中单独比较id列.它没有使用你的变量.考虑将变量名称更改为v_id
select max(x), min(x), max(y), min(y) into MAX_X, MIN_X, MAX_Y, MIN_Y
from tmp_bounding_box where id in ( v_id );
Run Code Online (Sandbox Code Playgroud)