Oracle使用由字串索引的二維陣列
–參考文章:http://www.oracle.com/technetwork/issue-archive/2014/14-sep/o54plsql-2245345.html
–SAMPLE DATA
店名 商品 銷量
A APPLE 3000
A BANANA 2000
B APPLE 5000
B MELON 100
–CONSTRUCT PACKAGE
CREATE OR REPLACE PACKAGE p_nested_array IS
SUBTYPE prod_name IS VARCHAR2(200);
SUBTYPE store_name IS VARCHAR2(200);
c_was_processed CONSTANT BOOLEAN := TRUE;
TYPE t_prod_qty IS TABLE OF NUMBER(10) INDEX BY prod_name;
TYPE t_store_prod_qty IS TABLE OF t_prod_qty INDEX BY store_name;
store_prod_qty_t t_store_prod_qty;
PROCEDURE reset_store_prod_qty;
FUNCTION already_store_prod(store_name_in VARCHAR2,
prod_name_in VARCHAR2) RETURN BOOLEAN;
PROCEDURE add_store_prod_qty(store_name_in VARCHAR2,
prod_name_in VARCHAR2,
qty_in NUMBER);
FUNCTION find_store_prod_qty(store_name_in VARCHAR2,
prod_name_in VARCHAR2) RETURN NUMBER;
PROCEDURE print_store_prod_qty;
END p_nested_array;
/
CREATE OR REPLACE PACKAGE BODY p_nested_array IS
PROCEDURE reset_store_prod_qty IS
BEGIN
store_prod_qty_t.DELETE;
END reset_store_prod_qty;
FUNCTION already_store_prod(store_name_in VARCHAR2,
prod_name_in VARCHAR2) RETURN BOOLEAN IS
BEGIN
IF store_prod_qty_t(store_name_in) (prod_name_in) IS NOT NULL THEN
RETURN TRUE;
ELSE
RETURN FALSE;
END IF;
EXCEPTION
WHEN no_data_found THEN
RETURN FALSE;
END already_store_prod;
PROCEDURE add_store_prod_qty(store_name_in VARCHAR2,
prod_name_in VARCHAR2,
qty_in NUMBER) IS
BEGIN
store_prod_qty_t(store_name_in)(prod_name_in) := qty_in;
END add_store_prod_qty;
FUNCTION find_store_prod_qty(store_name_in VARCHAR2,
prod_name_in VARCHAR2) RETURN NUMBER IS
BEGIN
RETURN store_prod_qty_t(store_name_in)(prod_name_in);
EXCEPTION
WHEN no_data_found THEN
RETURN NULL;
END find_store_prod_qty;
PROCEDURE print_store_prod_qty IS
store_name_idx store_name;
prod_name_idx prod_name;
BEGIN
store_name_idx := store_prod_qty_t.FIRST;
dbms_output.put_line(‘store_name_idx: ’ || store_name_idx);
WHILE store_name_idx IS NOT NULL LOOP
prod_name_idx := store_prod_qty_t(store_name_idx).FIRST;
–(store_prod_qty_t(store_name_idx).FIRST);
WHILE prod_name_idx IS NOT NULL LOOP
dbms_output.put_line(store_name_idx || ‘–’ || prod_name_idx || ‘: ’ ||
store_prod_qty_t(store_name_idx) (prod_name_idx));
prod_name_idx := store_prod_qty_t(store_name_idx).NEXT(prod_name_idx);
–(store_prod_qty_t(store_name_idx).NEXT(prod_name_idx));
END LOOP;
store_name_idx := store_prod_qty_t.NEXT(store_name_idx);
END LOOP;
/* error raised
FOR i IN store_prod_qty_t.FIRST .. store_prod_qty_t.LAST LOOP
FOR j IN store_prod_qty_t(i).FIRST .. store_prod_qty_t(i).LAST LOOP
dbms_output.put_line('Store ' || i || ' saled ' || store_prod_qty_t(i)
(j) || ' ' || j || '.');
END LOOP;
END LOOP;*/
END print_store_prod_qty;
END p_nested_array;
/
–TEST SCRIPT
DECLARE
BEGIN
P_NESTED_ARRAY.RESET_STORE_PROD_QTY;
P_NESTED_ARRAY.ADD_STORE_PROD_QTY(‘A’,’APPLE’,3000);
P_NESTED_ARRAY.ADD_STORE_PROD_QTY(‘A’,’BANANA’,2000);
P_NESTED_ARRAY.ADD_STORE_PROD_QTY(‘B’,’APPLE’,5000);
IF NOT P_NESTED_ARRAY.ALREADY_STORE_PROD(‘B’,’MELON’) THEN
P_NESTED_ARRAY.ADD_STORE_PROD_QTY(‘B’,’MELON’,100);
ELSE
DBMS_OUTPUT.PUT_LINE(‘Store B”s MELON saled ‘||P_NESTED_ARRAY.FIND_STORE_PROD_QTY(‘B’,’MELON’)||’.’);
END IF;
DBMS_OUTPUT.PUT_LINE(‘Store B”s MELON saled ‘||P_NESTED_ARRAY.FIND_STORE_PROD_QTY(‘B’,’MELON’)||’.’);
P_NESTED_ARRAY.print_store_prod_qty;
END;
/
–OUTPUT:
Store B’s MELON saled 100.
store_name_idx: A
A–APPLE: 3000
A–BANANA: 2000
B–APPLE: 5000
B–MELON: 100
—-Dylan Presents.
相關文章
- java之陣列的索引,排序以及二維陣列Java陣列索引排序
- JavaSE 陣列:一維陣列&二維陣列Java陣列
- 二維陣列陣列
- js 一維陣列轉二維陣列JS陣列
- js 二維陣列轉一維陣列JS陣列
- PHP二維陣列轉一維陣列PHP陣列
- C/C++ 二維陣列的理解(多維陣列)C++陣列
- javascript陣列可以使用字串作為索引JavaScript陣列字串索引
- PHP中二維陣列與多維陣列PHP陣列
- JavaScript --二維陣列查詢一維陣列JavaScript陣列
- vector 二維陣列陣列
- Java二維陣列Java陣列
- JavaScript二維陣列JavaScript陣列
- JavaScript 二維陣列JavaScript陣列
- 二維陣列排序陣列排序
- JavaScript二維陣列轉換成一維陣列JavaScript陣列
- PHP二維陣列切割為字串並去除重複的值PHP陣列字串
- C++ 使用 new 建立二維陣列C++陣列
- 二維陣列的計算陣列
- 二維陣列的獲取陣列
- C++動態建立二維陣列,二維陣列指標,以及動態二維陣列函式傳遞C++陣列指標函式
- 求二維陣列中最大子陣列的和陣列
- 二維陣列和稀疏陣列互轉陣列
- 二維陣列查詢陣列
- php 二維陣列排序PHP陣列排序
- 二維陣列行排序陣列排序
- perl操作二維陣列陣列
- 二維陣列傳參陣列
- 二維樹狀陣列陣列
- 二維陣列練習陣列
- Java 二維陣列轉一維Java陣列
- 指標陣列和陣列指標與二維陣列指標陣列
- 二維陣列中的查詢陣列
- 二維陣列的指標的理解陣列指標
- C的二維陣列(習題)陣列
- PHP一維陣列轉二維陣列正規表示式PHP陣列
- 淺談二維陣列在傳參中的使用陣列
- php中使用while遍歷二維陣列的方法PHPWhile陣列