PL/SQL数据结构,例如Hashmap [英] PL/SQL datastructure like Hashmap
问题描述
Oracle pl/sql中有一种方法可以创建像java Hashmap这样的数据结构,该结构可以将记录类型作为值,并将String作为索引.
Is there a way in Oracle pl/sql that I can create a datastructure like java Hashmap which can have a record type as value and String as index.
例如;
type emp_rec_type is record (emp_id number,emp_salary number);
emp_rec emp_rec_type;
type emp_assoc_array is table of emp_rec_type indexed by varchar2(30);
emp_map emp_assoc_array;
我希望能够做到以下
emp_rec.emp_id := 1;
emp_rec.salary := 1000;
emp_map('Rohan') := emp_rec;
这可以实现吗? 我不能使用嵌套表,因为我只能按整数对其进行索引. 对于关联数组,我不能将对象类型用作属性.
Can this be achieved? I cannot use nested tables since I can only index it by integer. With associative arrays, I cannot use object type as attributes.
也希望这样做,而不必创建对象或模式数据类型. 你能建议点什么吗?
Also would like to do this without having to create an object or schema datatype. Can you please suggest something?
推荐答案
In PL/SQL you can define associative arrays indexed by VARCHAR2
(since 10g I think):
SQL> DECLARE
2 TYPE map_varchar IS TABLE OF VARCHAR2(30) INDEX BY VARCHAR2(30);
3 l map_varchar;
4 BEGIN
5 l('A') := 'alpha';
6 l('B') := 'beta';
7 dbms_output.put_line(l('A'));
8 END;
9 /
alpha
内部,我认为结构更像是二叉树映射而不是哈希映射.
Internally, I think the structure is more like a binary-tree map than an hashmap.
您当然可以存储PL/SQL记录或SQL对象:
Of course you can store PL/SQL records or SQL objects:
SQL> DECLARE
2 TYPE emp_rec_type IS RECORD (emp_id NUMBER,emp_salary NUMBER);
3 TYPE map_varchar IS TABLE OF emp_rec_type INDEX BY VARCHAR2(30);
4 l_emp emp_rec_type;
5 l_map map_varchar;
6 BEGIN
7 l_emp.emp_id := 1;
8 l_emp.emp_salary := 1000;
9 l_map('A') := l_emp;
10 -- you can also affect record values directly
11 l_map('B').emp_id := 2;
12 l_map('B').emp_salary := 2000;
13 dbms_output.put_line(l_map('A').emp_salary);
14 END;
15 /
1000
PL/SQL procedure successfully completed
这篇关于PL/SQL数据结构,例如Hashmap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!