如何在oracle中为记录类型编写循环

发布于 2024-09-10 19:04:56 字数 439 浏览 3 评论 0原文

我有以下语句,可以在我的包中正常编译:

package header:

   TYPE role_user_type IS RECORD (
      ROLE_ID        some_table.ROLE_ID%TYPE,
      SUBGROUP       some_table.USER_ID%TYPE
   );

body:

ROLE_USER_REC                  MY_PACKAGE.ROLE_USER_TYPE;

SELECT B.USER_ID, B.ROLE INTO ROLE_USER_REC
FROM some_table where user_id like 'M%'

循环 ROLE_USER_REC 的骨架是什么?我们甚至可以循环遍历它吗?

I have the following statement which compiles fine in my package:

package header:

   TYPE role_user_type IS RECORD (
      ROLE_ID        some_table.ROLE_ID%TYPE,
      SUBGROUP       some_table.USER_ID%TYPE
   );

body:

ROLE_USER_REC                  MY_PACKAGE.ROLE_USER_TYPE;

SELECT B.USER_ID, B.ROLE INTO ROLE_USER_REC
FROM some_table where user_id like 'M%'

what is the skeleton for looping through ROLE_USER_REC? can we even loop through it?

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

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

发布评论

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

评论(3

等往事风中吹 2024-09-17 19:04:57

您可以为此使用游标

FOR i in (/* Your Select query*/)
loop
/* You can use value of the column fetched from select query like i.column_name
and use it which ever way you want */
end loop;

You can use cursors for this

FOR i in (/* Your Select query*/)
loop
/* You can use value of the column fetched from select query like i.column_name
and use it which ever way you want */
end loop;
在你怀里撒娇 2024-09-17 19:04:56

没有什么可以循环的。

role_user_type 定义一条记录,您可以通过以下方式访问该记录:

dbms_output.put_line( role_user_rec.role_id || ', ' || role_user_rec.subgroup );

一旦返回多行,您的 SELECT ... INTO 将失败。


如果您需要存储其中多个记录,可以使用嵌套表,例如
TYPE role_user_tab 是 role_user_type 的表

示例

DECLARE
  TYPE role_user_type IS RECORD (
     ROLE_ID        VARCHAR2(10),
     SUBGROUP       VARCHAR2(10)
  );
  TYPE role_user_tab IS TABLE OF role_user_type;
  role_user_rec role_user_tab;
BEGIN
  SELECT 'A', 'B'
  BULK COLLECT INTO role_user_rec
  FROM dual;
  FOR i IN role_user_rec.FIRST .. role_user_rec.LAST LOOP
    dbms_output.put_line( role_user_rec(i).role_id || ', ' || role_user_rec(i).subgroup );
  END LOOP;
END;

There is nothing to loop.

role_user_type defines a single record, that you can access via:

dbms_output.put_line( role_user_rec.role_id || ', ' || role_user_rec.subgroup );

Your SELECT ... INTO will fail as soon as more than one row is returned.


If you need to store several of those records, you can use nested tables like
TYPE role_user_tab IS TABLE OF role_user_type:

Example:

DECLARE
  TYPE role_user_type IS RECORD (
     ROLE_ID        VARCHAR2(10),
     SUBGROUP       VARCHAR2(10)
  );
  TYPE role_user_tab IS TABLE OF role_user_type;
  role_user_rec role_user_tab;
BEGIN
  SELECT 'A', 'B'
  BULK COLLECT INTO role_user_rec
  FROM dual;
  FOR i IN role_user_rec.FIRST .. role_user_rec.LAST LOOP
    dbms_output.put_line( role_user_rec(i).role_id || ', ' || role_user_rec(i).subgroup );
  END LOOP;
END;
疧_╮線 2024-09-17 19:04:56

您可以使用游标 FOR 循环:

BEGIN
  FOR role_user_type IN ('SELECT B.USER_ID, B.ROLE FROM some_table where user_id like ''M%'')
  LOOP
    dbms_output.put_line('User ID: '||role_user_type.user_id);
    etc...
  END LOOP;
END;

另一种选择:

DECLARE
  CURSOR C IS
SELECT B.USER_ID, B.ROLE 
  FROM some_table 
 where user_id like 'M%';
BEGIN
  FOR role_user_type IN C LOOP
    dbms_output.put_line('User ID: '||role_user_type.user_id);
    etc...
  END LOOP;
END;

You can use a cursor FOR loop:

BEGIN
  FOR role_user_type IN ('SELECT B.USER_ID, B.ROLE FROM some_table where user_id like ''M%'')
  LOOP
    dbms_output.put_line('User ID: '||role_user_type.user_id);
    etc...
  END LOOP;
END;

Another alternative:

DECLARE
  CURSOR C IS
SELECT B.USER_ID, B.ROLE 
  FROM some_table 
 where user_id like 'M%';
BEGIN
  FOR role_user_type IN C LOOP
    dbms_output.put_line('User ID: '||role_user_type.user_id);
    etc...
  END LOOP;
END;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文