Oracle 中VARRAY的 NOT NULL之惑

來源:互聯網
上載者:User

如果在定義VARRAY的時候帶上NOT NULL限制,那麼這個VARRAY的元素就不能為NULL.

如下定義:

CREATE OR REPLACE TYPE integer_varray
  AS VARRAY(5) OF INTEGER NOT NULL;
/

然後有一個PLSQL塊如下:

DECLARE

  -- Declare and initialize a null set of rows.
  varray_integer INTEGER_VARRAY := integer_varray();

BEGIN

  -- Loop through all records to print the varray contents.
  FOR i IN 1..varray_integer.LIMIT LOOP

    -- Initialize row.
    varray_integer.EXTEND;

 /*沒有賦值,如果不賦值是NULL的話,應該編譯錯誤啊,結果是順利通過編譯*/

  END LOOP;

 

    -- Print to console how many rows are initialized.
    dbms_output.put     ('Integer Varray Initialized ');
    dbms_output.put_line('['||varray_integer.COUNT||']');
   
    --varray_integer(1):=null;
   
    FOR i IN 1..varray_integer.COUNT LOOP

    -- Print the contents.
    dbms_output.put     ('Integer Varray ['||i||'] ');
    dbms_output.put_line('['||varray_integer(i)||']');
   
    if(varray_integer(i) is null) then

   /*本來是認為這裡應該不執行,結果會列印出來*/
          dbms_output.put_line('Integer Varray ['||i||'] '|| 'is null');
    end if;

  END LOOP;

END;
/

運行結果如下:/*運行也正常,顯示元素為NULL,於定義矛盾*/

Integer Varray Initialized [5]
Integer Varray [1] []
Integer Varray [1] is null
Integer Varray [2] []
Integer Varray [2] is null
Integer Varray [3] []
Integer Varray [3] is null
Integer Varray [4] []
Integer Varray [4] is null
Integer Varray [5] []
Integer Varray [5] is null

測試環境為:

BANNER
----------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod
PL/SQL Release 10.2.0.1.0 - Production
CORE    10.2.0.1.0      Production
TNS for 32-bit Windows: Version 10.2.0.1.0 - Production
NLSRTL Version 10.2.0.1.0 - Production

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.