﻿ PL/SQL Challenge 每日一题：2015-1-9 使用负数索引的关联数组 - 鸿网互联

# PL/SQL Challenge 每日一题：2015-1-9 使用负数索引的关联数组

http://www.itpub.net/forum.php?m ... eid&typeid=1808

http://www.plsqlchallenge.com/

CREATE OR REPLACE PROCEDURE plch_mark_for_deletion (
list_io   IN OUT NOCOPY plch_pkg.list_t)
IS
BEGIN
FOR indx IN 1 .. list_io.COUNT
LOOP
IF MOD (indx, 2) = 0
THEN
list_io (-1 * indx) := list_io (indx);
list_io.delete (indx);
END IF;
END LOOP;
END;
/

DECLARE
l_list   plch_pkg.list_t := plch_pkg.initialize ();
BEGIN
plch_mark_for_deletion (l_list);

FOR indx IN l_list.FIRST .. -1
LOOP
l_list.delete (indx);
END LOOP;

DBMS_OUTPUT.put_line ('Count = ' || l_list.COUNT);
END;
/

(A)
CREATE OR REPLACE PACKAGE plch_pkg
IS
TYPE list_t IS TABLE OF INTEGER
INDEX BY PLS_INTEGER;

FUNCTION initialize
RETURN list_t;
END;
/

CREATE OR REPLACE PACKAGE BODY plch_pkg
IS
FUNCTION initialize
RETURN list_t
IS
l_list   list_t;
BEGIN
SELECT LEVEL
BULK COLLECT INTO l_list
FROM DUAL
CONNECT BY LEVEL < 10;

RETURN l_list;
END;
END;
/

(B)
CREATE OR REPLACE PACKAGE plch_pkg
IS
TYPE list_t IS TABLE OF INTEGER;

FUNCTION initialize RETURN list_t;
END;
/

CREATE OR REPLACE PACKAGE BODY plch_pkg
IS
FUNCTION initialize
RETURN list_t
IS
l_list   list_t;
BEGIN
SELECT LEVEL
BULK COLLECT INTO l_list
FROM DUAL
CONNECT BY LEVEL < 10;

RETURN l_list;
END;
END;
/

(C)
CREATE OR REPLACE PACKAGE plch_pkg
IS
TYPE list_t IS VARRAY(10) OF INTEGER;

FUNCTION initialize RETURN list_t;
END;
/

CREATE OR REPLACE PACKAGE BODY plch_pkg
IS
FUNCTION initialize
RETURN list_t
IS
l_list   list_t;
BEGIN
SELECT LEVEL
BULK COLLECT INTO l_list
FROM DUAL
CONNECT BY LEVEL < 10;

RETURN l_list;
END;
END;
/

(D)
CREATE OR REPLACE PACKAGE plch_pkg
IS
TYPE list_t IS TABLE OF VARCHAR2(100)
INDEX BY VARCHAR2(100);

FUNCTION initialize
RETURN list_t;
END;
/

CREATE OR REPLACE PACKAGE BODY plch_pkg
IS
FUNCTION initialize
RETURN list_t
IS
l_list   list_t;
BEGIN
SELECT LEVEL
BULK COLLECT INTO l_list
FROM DUAL
CONNECT BY LEVEL < 10;

RETURN l_list;
END;
END;
/

<