访问PLOSCAR集合的数据

访问集合数据时,对于嵌套表和变长数组元素我们应该确保对集合已经做了相关的扩展等操作,能够容纳我们要访问的索引。

以下示例展示了对集合变量的一些简单操作

示例:集合变量的简单操作

--清理环境
DROP PROCEDURE proc;

CREATE OR REPLACE PROCEDURE proc AS
DECLARE
    TYPE population IS TABLE OF INT INDEX BY VARCHAR2(64);
    city_population population;
    city_population2 population;
    i VARCHAR2(64);
BEGIN
    city_population('Smallville') := 2000;
    city_population('Midland') := 750000;
    city_population('Megalopolis') := 1000000;
    city_population('Smallville') := 2001;
    i := city_population.FIRST;
    WHILE i IS NOT NULL LOOP
        dbms_output.put_line( 'Population of ' || i || ' is ' ||
                    TO_CHAR(city_population(i)));
        i := city_population.NEXT(i);
    END LOOP;
    city_population2 := city_population;
    city_population('Smallville') := 2002;
    city_population('Midland') := 850000;
    city_population('Megalopolis') := 2200000;
    i := city_population2.FIRST;
    WHILE i IS NOT NULL LOOP
        dbms_output.put_line( 'Population2 of ' || i || ' is ' || 
                    TO_CHAR(city_population(i)));
        i := city_population.NEXT(i);
    END LOOP;
END;

/
EXEC proc;
Population of Megalopolis is 1000000
Population of Midland is 750000
Population of Smallville is 2001
Population2 of Megalopolis is 2200000
Population2 of Midland is 850000
Population2 of Smallville is 2002

--删除
DROP PROCEDURE proc;