views:

342

answers:

2

hi, i have a type as follows:

CREATE OR REPLACE TYPE tbusiness_inter_item_bag AS OBJECT (
   item_id              NUMBER,
   system_event_cd      VARCHAR2 (20),
   CONSTRUCTOR FUNCTION tbusiness_inter_item_bag RETURN SELF AS RESULT
);
CREATE OR REPLACE TYPE BODY tbusiness_inter_item_bag
AS
   CONSTRUCTOR FUNCTION tbusiness_inter_item_bag RETURN SELF AS RESULT
   AS
   BEGIN
      RETURN;
   END;
END;

when i execute the following script, i got a "Reference to uninitialized composite" error, which is imho quite suitable.

DECLARE
   item   tbusiness_inter_item_bag;
BEGIN
   item.system_event_cd := 'ABC';
END;

This also raises the same error:

item.item_id := 3;

But if i change my object type into:

CREATE OR REPLACE TYPE tbusiness_inter_item_bag AS OBJECT (
   item_id              NUMBER(1),
   system_event_cd      VARCHAR2 (20),
   CONSTRUCTOR FUNCTION tbusiness_inter_item_bag RETURN SELF AS RESULT
);

then the last statement raises no more error (where my "item" is still uninitialized):

item.item_id := 3;

Shouldn't i get the same ORA-06530 error?

ps: Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bi

A: 

Hi mutoss,

You need to call the constructor you defined:

SQL> DECLARE
  2     item   tbusiness_inter_item_bag := tbusiness_inter_item_bag();
  3     /*                                 ^^ call the constructor */
  4  BEGIN
  5     item.system_event_cd := 'ABC';
  6  END;
  7  /

PL/SQL procedure successfully completed

I observe the behaviour you described on a 10.2.0.3 database. I wouldn't rely on it though, it looks like a bug.

Vincent Malgrat
Vincent, I think you have missed the point. The assignment to item.item_id succeeds or fails depending on whether the numeric attibute is defined with precision. This behaviour is inconsistent, and hence probably a bug.
APC
@APC: yes, I've realized this is the object of the question. I updated my answer to reflect my observations (this looks like a bug: don't rely on it :)
Vincent Malgrat
yes this looked like a bug to me too, i was just thinking to warn my colleagues here to not to rely on it :) I posted it to be sure..
mutoss
+2  A: 

I reproduced the same behaviour in Oracle 11gR1. I would agree with you, this seems like a bug to me too, albeit a trivial one.

SQL> CREATE OR REPLACE TYPE tbusiness_inter_item_bag AS OBJECT (
  2     item_id              NUMBER(1),
  3     system_event_cd      VARCHAR2 (20),
  4     CONSTRUCTOR FUNCTION tbusiness_inter_item_bag RETURN SELF AS RESULT
  5  );
  6  /

Type created.

SQL> DECLARE
  2     item   tbusiness_inter_item_bag;
  3  BEGIN
  4     item.item_id := 1;
  5  END;
  6  /

PL/SQL procedure successfully completed.

SQL>

Note that this still fails:

SQL> DECLARE
  2     item   tbusiness_inter_item_bag;
  3  BEGIN
  4     item.item_id := 1;
  5     item.system_event_cd := 'ABC';
  6  END;
  7  /
DECLARE
*
ERROR at line 1:
ORA-06530: Reference to uninitialized composite
ORA-06512: at line 5

SQL>

Obviously, the correct practice is always initialize objects before referencing them.

SQL> DECLARE
  2     item   tbusiness_inter_item_bag := tbusiness_inter_item_bag();
  3  BEGIN
  4     item.system_event_cd  := 'ABC';
  5  END;
  6  /

PL/SQL procedure successfully completed.

SQL>
APC
thank you, that was what i expected.
mutoss