gpt4 book ai didi

sql - 如何在 Oracle PL SQL 中定义类似字典的结构?

转载 作者:行者123 更新时间:2023-12-04 15:58:22 26 4
gpt4 key购买 nike

如何在PL/SQL中定义如下结构:

包含多行的字符串列表。

例子:

'User A'
-->
(1)
-->
1
(2)
-->
2


'User B'
-->
(1)
-->
0
(2)
-->
9

整数定义为:

TYPE number_arry IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;

如何定义整个结构?

我想从这个表中填充这个结构:

RowId | User_A | User_B  
------+--------+--------
1 | 1 | 0
2 | 2 | 0
3 | 3 | 9

使用这些语句:

CURSOR c1
IS
SELECT User_A, User_B FROM my_table;

OPEN c1;
LOOP
FETCH c1
BULK COLLECT INTO
my_dict('User A'),
my_dict('User B')

LIMIT 1000;

EXIT WHEN c1%NOTFOUND;
END LOOP;

最佳答案

您可以使用以下结构:

SQL> CREATE TABLE my_table AS
2 SELECT 1 user_a, 0 user_b FROM dual
3 UNION ALL SELECT 2, 0 FROM dual
4 UNION ALL SELECT 3, 9 FROM dual;

Table created

SQL> DECLARE
2 CURSOR c1 IS
3 SELECT User_A, User_B FROM my_table;
4 TYPE number_arry IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
5 TYPE dictionary_type IS TABLE OF number_arry INDEX BY VARCHAR2(30);
6 l_dico dictionary_type;
7 BEGIN
8 OPEN c1;
9 LOOP
10 FETCH c1 BULK COLLECT
11 INTO l_dico('User A'), l_dico('User B') LIMIT 1000;
12 EXIT WHEN c1%NOTFOUND;
13 END LOOP;
14 CLOSE c1;
15 END;
16 /

PL/SQL procedure successfully completed

关于sql - 如何在 Oracle PL SQL 中定义类似字典的结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6276573/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com