手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

Oracle 11g Release 1 (11.1) PL/SQL_理解 Collection 类型

[日期:2012-06-24] 来源:Linux社区  作者:liuning8023 [字体:大 中 小]

引用 Collection 元素

引用元素包括一个 collection 名字和用大括号括起来的标值(subscript)。通过下面语法:

collection_name (subscript)

这里的“标值”,通常,是一个返回整数值的表达式,或用字符串声明的 associative arrays 键,它是一个 VARCHAR2。

标值的范围如下:

  • 对于 nested tables,1..2147483647 (上限是 PLS_INTEGER)。
  • 对于 varrays,1.. size_limit,你在声明中指定的大小,size_limit 不能超过 2147483647。
  • 对于带数字型键的 associative arrays,-2147483648..2147483647。
  • 对于带字符串键的 associative arrays,键的长度和可能值的数量依赖于类型声明中 VARCHAR2 的限制,和数据库字符集。

示例 12:演示引用 nested table 元素

DECLARE
  TYPE Roster IS TABLE OF VARCHAR2(15);
  names Roster := 
    Roster('D Caruso', 'J Hamil', 'D Piro', 'R Singh');
  PROCEDURE verify_name(the_name VARCHAR2) IS
  BEGIN
    DBMS_OUTPUT.PUT_LINE(the_name);
  END;
BEGIN
  FOR i IN names.FIRST .. names.LAST
  LOOP
      IF names(i) = 'J Hamil' THEN
        DBMS_OUTPUT.PUT_LINE(names(i));
          -- reference to nested table element
      END IF;
  END LOOP;
  verify_name(names(3));
    -- procedure call with reference to element
END;
/

示例 13:演示引用 associative array 元素

DECLARE
  TYPE sum_multiples IS TABLE OF PLS_INTEGER INDEX BY PLS_INTEGER;
  n  PLS_INTEGER := 5;   -- number of multiples to sum for display
  sn PLS_INTEGER := 10;  -- number of multiples to sum
  m  PLS_INTEGER := 3;   -- multiple
FUNCTION get_sum_multiples
  (multiple IN PLS_INTEGER, num IN PLS_INTEGER)
  RETURN sum_multiples IS
  s sum_multiples;
  BEGIN
      FOR i IN 1..num LOOP
        s(i) := multiple * ((i * (i + 1)) / 2);
           -- sum of multiples
      END LOOP;
    RETURN s;
  END get_sum_multiples;
BEGIN
-- invoke function to retrieve
-- element identified by subscript (key)
  DBMS_OUTPUT.PUT_LINE
    ('Sum of the first ' || TO_CHAR(n) || ' multiples of ' ||
     TO_CHAR(m) || ' is ' || TO_CHAR(get_sum_multiples (m, sn)(n)));
END;
/

 

给 Collection 赋值

一个 collection 可以通过 INSERT、UPDATE、FETCH 或 SELECT 语句赋值。通过下面语法,把表达式值赋给 collection 中的一个指定元素:

collection_name (subscript) := expression;

其中,表达式值与类型兼容。

可以使用 SET、MULTISET UNION、MULTISET INTERSECT 和 MULTISET EXCEPT 操作符把 nested tables 转换成赋值语句的一部分。

下面情况,赋一个值给 collection 元素会产生异常:

  • 若标值为 NULL 或不能转换成正确的数据类型,PL/SQL 会产生异常 VALUE_ERROR。通常,标值必须是整数。associative arrays 也可以有 VARCHAR2 标值。
  • 若标值引用一个为初始化的元素,则 PL/SQL 会产生异常 SUBSCRIPT_BEYOND_COUNT。
  • 若 collection (自动)为 null,则 PL/SQL 会产生异常 COLLECTION_IS_NULL。

示例 14:演示 Collection 类型兼容赋值

DECLARE
   TYPE last_name_typ IS VARRAY(3) OF VARCHAR2(64);
   TYPE surname_typ IS VARRAY(3) OF VARCHAR2(64);
-- These first two variables have the same data type.
   group1 last_name_typ := last_name_typ('Jones','Wong','Marceau');
   group2 last_name_typ := last_name_typ('Klein','Patsos','Singh');
-- This third variable has a similar declaration,
-- but is not the same type.
   group3 surname_typ := surname_typ('Trevisi','Macleod','Marquez');
BEGIN
-- Allowed because they have the same data type
   group1 := group2;
-- Not allowed because they have different data types
--   group3 := group2; -- raises an exception
END;
/

示例 15:演示 nested table 赋值为 Null。

若把一个自动为 null 的 nested table 或 varray 赋值给另一个 nested table 或 varray,则另一个 collection 必须重新初始化。同样,把一个 collection 赋值为 NULL,它自动为 null。

DECLARE
   TYPE dnames_tab IS TABLE OF VARCHAR2(30);
-- This nested table has some values
   dept_names dnames_tab :=
     dnames_tab('Shipping','Sales','Finance','Payroll');
-- This nested table is not initialized ("atomically null").
   empty_set dnames_tab;
BEGIN
-- At first, the initialized variable is not null.
   if dept_names IS NOT NULL THEN
      DBMS_OUTPUT.PUT_LINE('OK, at first dept_names is not null.');
   END IF;
-- Then assign a null nested table to it.
   dept_names := empty_set;
-- Now it is null.
   if dept_names IS NULL THEN
      DBMS_OUTPUT.PUT_LINE('OK, now dept_names has become null.');
   END IF;
-- Use another constructor to give it some values.
   dept_names := dnames_tab('Shipping','Sales','Finance','Payroll');
END;
/

示例 16:演示用 Set 操作符给 nested tables 赋值

可以对 nested tables 应用一些 ANSI 标准的操作符。

DECLARE
  TYPE nested_typ IS TABLE OF NUMBER;
  nt1 nested_typ := nested_typ(1,2,3);
  nt2 nested_typ := nested_typ(3,2,1);
  nt3 nested_typ := nested_typ(2,3,1,3);
  nt4 nested_typ := nested_typ(1,2,4);
  answer nested_typ;
-- The results might be in a different order than you expect.
-- Do not rely on the order of elements in nested tables.
  PROCEDURE print_nested_table(the_nt nested_typ) IS
     output VARCHAR2(128);
  BEGIN
     IF the_nt IS NULL THEN
        DBMS_OUTPUT.PUT_LINE('Results: <NULL>');
        RETURN;
     END IF;
     IF the_nt.COUNT = 0 THEN
        DBMS_OUTPUT.PUT_LINE('Results: empty set');
        RETURN;
     END IF;
     FOR i IN the_nt.FIRST .. the_nt.LAST
     LOOP
        output := output || the_nt(i) || ' ';
     END LOOP;
     DBMS_OUTPUT.PUT_LINE('Results: ' || output);
  END;
BEGIN
  answer := nt1 MULTISET UNION nt4; -- (1,2,3,1,2,4)
  print_nested_table(answer);
  answer := nt1 MULTISET UNION nt3; -- (1,2,3,2,3,1,3)
  print_nested_table(answer);
  answer := nt1 MULTISET UNION DISTINCT nt3; -- (1,2,3)
  print_nested_table(answer);
  answer := nt2 MULTISET INTERSECT nt3; -- (3,2,1)
  print_nested_table(answer);
  answer := nt2 MULTISET INTERSECT DISTINCT nt3; -- (3,2,1)
  print_nested_table(answer);
  answer := SET(nt3); -- (2,3,1)
  print_nested_table(answer);
  answer := nt3 MULTISET EXCEPT nt2; -- (3)
  print_nested_table(answer);
  answer := nt3 MULTISET EXCEPT DISTINCT nt2; -- ()
  print_nested_table(answer);
END;
/
linux
相关资讯       Oracle存储过程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款