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

发布于 2024-11-14 07:34:33 字数 943 浏览 3 评论 0原文

如何在 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;

How to define in PL/SQL the following structure:

A list of strings which contains multiple rows.

Example:

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


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

The integers are defined as:

TYPE number_arry IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;

How to define the whole structure ?

I want to fill this structure from this table:

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

With these statements:

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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

坚持沉默 2024-11-21 07:34:33

您可以使用以下结构:

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

You could use the following structure:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文