首页 > 其他 > 详细

PL学习-Collection

时间:2020-05-15 21:38:03      阅读:59      评论:0      收藏:0      [点我收藏+]

Collectionn基础

collection有三个

  1. 关联数组,varray,嵌套表
  2. 关联数组不能再schema级别定义,不需要初始化
  3. empty和null的概念
  4. varray和嵌套表可以再schema层面使用,默认null,使用前需要初始化

 

关联数组-associative array

技术分享图片

 

 

CREATE OR REPLACE PACKAGE My_Types AUTHID CURRENT_USER IS
  TYPE My_AA IS TABLE OF VARCHAR2(20) INDEX BY PLS_INTEGER;
  FUNCTION Init_My_AA RETURN My_AA;
END My_Types;
/
CREATE OR REPLACE PACKAGE BODY My_Types IS
  FUNCTION Init_My_AA RETURN My_AA IS
    Ret My_AA;
  BEGIN
    Ret(-10) := -ten;
    Ret(0) := zero;
    Ret(1) := one;
    Ret(2) := two;
    Ret(3) := three;
    Ret(4) := four;
    Ret(9) := nine;
    RETURN Ret;
  END Init_My_AA;
END My_Types;
/
DECLARE
  v CONSTANT My_Types.My_AA := My_Types.Init_My_AA();
BEGIN
  DECLARE
    Idx PLS_INTEGER := v.FIRST();
  BEGIN
    WHILE Idx IS NOT NULL LOOP
      DBMS_OUTPUT.PUT_LINE(TO_CHAR(Idx, 999)||LPAD(v(Idx), 7));
      Idx := v.NEXT(Idx);
    END LOOP;
  END;
END;
/

 

 


 

没有null,自动变成empty

需要自己写初始化函数

 

index by得变量类型有限制,不是随意得

 

可变数组

技术分享图片

 

DECLARE
  TYPE Foursome IS VARRAY(4) OF VARCHAR2(15);  -- VARRAY type
 
  -- varray variable initialized with constructor:
 
  team Foursome := Foursome(John, Mary, Alberto, Juanita);
 
  PROCEDURE print_team (heading VARCHAR2) IS
  BEGIN
    DBMS_OUTPUT.PUT_LINE(heading);
 
    FOR i IN 1..4 LOOP
      DBMS_OUTPUT.PUT_LINE(i || . || team(i));
    END LOOP;
 
    DBMS_OUTPUT.PUT_LINE(---); 
  END;
  
BEGIN 
  print_team(2001 Team:);
 
  team(3) := Pierre;  -- Change values of two elements
  team(4) := Yvonne;
  print_team(2005 Team:);
 
  -- Invoke constructor to assign new values to varray variable:
 
  team := Foursome(Arun, Amitha, Allan, Mae);
  print_team(2009 Team:);
END;
/

 

 

嵌套表

技术分享图片

CREATE OR REPLACE TYPE nt_type IS TABLE OF NUMBER;
/
CREATE OR REPLACE PROCEDURE print_nt (nt nt_type) AUTHID DEFINER IS
  i  NUMBER;
BEGIN
  i := nt.FIRST;
 
  IF i IS NULL THEN
    DBMS_OUTPUT.PUT_LINE(nt is empty);
  ELSE
    WHILE i IS NOT NULL LOOP
      DBMS_OUTPUT.PUT(nt.( || i || ) = );
      DBMS_OUTPUT.PUT_LINE(NVL(TO_CHAR(nt(i)), NULL));
      i := nt.NEXT(i);
    END LOOP;
  END IF;
 
  DBMS_OUTPUT.PUT_LINE(---);
END print_nt;
/
DECLARE
  nt nt_type := nt_type();  -- nested table variable initialized to empty
BEGIN
  print_nt(nt);
  nt := nt_type(90, 9, 29, 58);
  print_nt(nt);
END;
/

 

构造器

collection_type ( [ value [, value ]... ] )

DECLARE
  TYPE Foursome IS VARRAY(4) OF VARCHAR2(15);
  team Foursome := Foursome();  -- initialize to empty
 
  PROCEDURE print_team (heading VARCHAR2)
  IS
  BEGIN
    DBMS_OUTPUT.PUT_LINE(heading);
 
    IF team.COUNT = 0 THEN
      DBMS_OUTPUT.PUT_LINE(Empty);
    ELSE 
      FOR i IN 1..4 LOOP
        DBMS_OUTPUT.PUT_LINE(i || . || team(i));
      END LOOP;
    END IF;
 
    DBMS_OUTPUT.PUT_LINE(---); 
  END;
 
BEGIN
  print_team(Team:);
  team := Foursome(John, Mary, Alberto, Juanita);
  print_team(Team:);
END;
/

 

 

嵌套表与SQL Multiset

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);
 
  PROCEDURE testify (
    truth BOOLEAN := NULL,
    quantity NUMBER := NULL
  ) IS
  BEGIN
    IF truth IS NOT NULL THEN
      DBMS_OUTPUT.PUT_LINE (
        CASE truth
           WHEN TRUE THEN True
           WHEN FALSE THEN False
        END
      );
    END IF;
    IF quantity IS NOT NULL THEN
        DBMS_OUTPUT.PUT_LINE(quantity);
    END IF;
  END;
BEGIN
  testify(truth => (nt1 IN (nt2,nt3,nt4)));        -- condition
  testify(truth => (nt1 SUBMULTISET OF nt3));      -- condition
  testify(truth => (nt1 NOT SUBMULTISET OF nt4));  -- condition
  testify(truth => (4 MEMBER OF nt1));             -- condition
  testify(truth => (nt3 IS A SET));                -- condition
  testify(truth => (nt3 IS NOT A SET));            -- condition
  testify(truth => (nt1 IS EMPTY));                -- condition
  testify(quantity => (CARDINALITY(nt3)));         -- function
  testify(quantity => (CARDINALITY(SET(nt3))));    -- 2 functions
END;
/

 

 

collection的方法

Method Type Description

DELETE

Procedure

Deletes elements from collection.

TRIM

Procedure

Deletes elements from end of varray or nested table.

EXTEND

Procedure

Adds elements to end of varray or nested table.

EXISTS

Function

Returns TRUE if and only if specified element of varray or nested table exists.

FIRST

Function

Returns first index in collection.

LAST

Function

Returns last index in collection.

COUNT

Function

Returns number of elements in collection.

LIMIT

Function

Returns maximum number of elements that collection can have.

PRIOR

Function

Returns index that precedes specified index.

NEXT

Function

Returns index that succeeds specified index.

 

技术分享图片

 
 

PL学习-Collection

原文:https://www.cnblogs.com/fqguo24/p/12896614.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!