PL/SQL中类似于地图的对象类型?

7 oracle collections plsql key map

我想在PL/SQL中编写类似地图的对象类型.我的意思是键值对列表,其中值可以是另一个键值对列表.简单,或者我想.以下是两个简化版

CREATE OR REPLACE TYPE TKey AS OBJECT
(
    name varchar2(240),
    value_text varchar2(2000),
    value_map TMap
)

CREATE OR REPLACE TYPE TMap AS TABLE OF TKey
Run Code Online (Sandbox Code Playgroud)

也许不那么简单,因为现在我有一个"鸡或鸡蛋"问题.如果我把TKey放在第一位,他会抱怨TMap没有定义.如果我先放入TMap,他会抱怨没有定义TKey.如果我把TKey放入并省略了value_map行,那么添加TMap类型,然后尝试替换TKey类型,他不会允许它.

我运气不好吗?这样的结构在PL/SQL中是不可能的吗?

提前致谢

澄清:我想要的东西,可以给我这个:包含键a,b和c的地图.a的值是varchar"hello",b的值是varchar"world",c的值是另一个映射,键是x和y,x的值是"what's"和值你是"向上".

这就是Java中的样子:

Map<String, Object> map = new HashMap<String, Object>();
map.set("a", "Hello");
map.set("b", "World");
Map<String, Object> child = new HashMap<String, Object>();
child.set("x", "What's");
child.set("y", "up");
map.set("c", child);
Run Code Online (Sandbox Code Playgroud)

现在我知道像"对象"这样的东西是无法存储的.我需要的是一个对象,它可以存储与该对象类型相同的对象列表.所以基本上,一棵树,是的.

mam*_*ing 16

您可以使用关联数组.从PL/SQL用户指南:

理解关联数组(索引表)

关联数组是键值对的集合,其中每个键都是唯一的,用于定位数组中的相应值.密钥可以是整数或字符串.

首次使用键分配值会将该键添加到关联数组中.使用相同密钥的后续分配更新相同的条目.选择一个独特的密钥非常重要.例如,键值可能来自数据库表的主键,数字散列函数或串联字符串以形成唯一的字符串值.

例如,下面是关联数组类型的声明,以及使用字符串键的两种类型的数组:

示例5-1声明集合类型

DECLARE  TYPE population_type IS TABLE OF NUMBER INDEX BY VARCHAR2(64);
  country_population population_type;
  continent_population population_type;
  howmany NUMBER;
  which VARCHAR2(64);
BEGIN
  country_population('Greenland') := 100000; -- Creates new entry
  country_population('Iceland') := 750000;   -- Creates new entry
-- Looks up value associated with a string
  howmany := country_population('Greenland');
  continent_population('Australia') := 30000000;
  continent_population('Antarctica') := 1000; -- Creates new entry
  continent_population('Antarctica') := 1001; -- Replaces previous value 
-- Returns 'Antarctica' as that comes first alphabetically.
  which := continent_population.FIRST;
-- Returns 'Australia' as that comes last alphabetically.  which := continent_population.LAST;
-- Returns the value corresponding to the last key, in this
-- case the population of Australia.
  howmany := continent_population(continent_population.LAST);
END;
/


Jef*_*emp 4

也许你需要更多地联系起来思考:)

无需将 TMap ( value_map) 存储在 TKey 类型中,只需存储即可value_map_name,然后可以使用它来查找表中的另一个条目。

CREATE OR REPLACE TYPE TKey AS OBJECT(
 name varchar2(240),
 value_text varchar2(2000),
 value_map_name varchar2(240));
Run Code Online (Sandbox Code Playgroud)

然后你可以使用关联数组(根据mamboking的答案)来存储它们。