Dưới đây là một đoạn trích từ một số mã PL/SQL mà tôi tin thể hiện một lỗi PL/SQL:Đây có phải là lỗi PL/SQL không?
if guid_ is null then
dbms_output.put_line('guid_ is null: ' || guid_);
end if;
Khi những dòng này được thực hiện, nó in
guid_ is null: 07D242FCC55000FCE0530A30D4928A21
Tôi đang trên Oracle 11R2
select * from v$version;
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
PL/SQL Release 11.2.0.4.0 - Production
CORE 11.2.0.4.0 Production
TNS for IBM/AIX RISC System/6000: Version 11.2.0.4.0 - Production
NLSRTL Version 11.2.0.4.0 - Production
Tôi có thể tạo lại điều này với các loại sau và khối ẩn danh. Xin lỗi vì chiều dài, nhưng tôi tin rằng tôi không thể rút ngắn nó nữa:
create type tq84_t as table of varchar2(32);
/
create type tq84_o as object (
dummy number(1),
not final member procedure clear
) not final;
/
show errors
create type tq84_d under tq84_o (
g varchar2(32),
constructor function tq84_d return self as result,
overriding member procedure clear
);
/
show errors
create package tq84_h as
t tq84_t;
end tq84_h;
/
show errors
create package body tq84_h as
begin
t := tq84_t();
end;
/
show errors
create type body tq84_o as
member procedure clear is begin
null;
end clear;
end;
/
create type body tq84_d as
constructor function tq84_d return self as result is
begin
g := sys_guid;
return;
end tq84_d;
overriding member procedure clear is begin
tq84_h.t.extend;
tq84_h.t(tq84_h.t.count) := g;
g := null;
end clear;
end;
/
show errors
declare
b tq84_o; -- Change to tq84_d ...
guid_ varchar2(32);
begin
b := new tq84_d;
guid_ := treat(b as tq84_d).g;
b.clear;
if guid_ is null then
dbms_output.put_line('guid_ is null: ' || guid_);
end if;
end;
/
drop type tq84_t;
drop type tq84_d;
drop type tq84_o;
drop package tq84_h;
Cũng lưu ý, đó là khi tôi thay đổi b tq84_o
-b tq84_d
, lỗi không xảy ra nữa.
Ai đó có thể xác minh xem điều này có xảy ra trên các hệ thống khác không?
Tôi không chắc chắn lý do tại sao đây là nêu ra, nhưng thay đổi 'b tq84_o' để' b tq84_d' gây 'guid_' là không rỗng khi' điều kiện if' được đánh giá. thêm câu lệnh 'else' để in một câu lệnh tương tự xác nhận điều đó. – Allan
Tôi chỉ nhận ra rằng lỗi là 'nếu guid_ được null' được đánh giá là true, không phải là 'cái dbms_output' được trả lại một giá trị khi biến nên được null. – Allan
Hơn nữa, thêm một 'else' làm cho phiên bản với 'b tq84_o' để hoạt động như mong đợi. – Allan