PL/SQL:从游标获取 2 个或更多连接表的最佳实践?

发布于 2024-07-13 19:27:23 字数 337 浏览 7 评论 0 原文

我听说使用 %ROWTYPE 属性在 PL/SQL 中定义记录是一个很好的做法。 这样可以节省打字时间,并且即使添加或删除列,您的包也可以继续运行。 (如果我错了,请纠正我!)

但是,当我从涉及连接的游标中获取数据时,我发现我必须获取程序员定义的记录,其中包括一个(很可能很长)手写列表连接返回的每一列。

所以我的问题是: 是否可以获取嵌套记录,或者获取记录列表,或者做一些事情来避免这种丑陋的混乱? 我尝试过的所有操作都会导致记录与游标返回的内容不匹配的错误。

使用游标返回连接结果对我来说似乎是一个常见的用例,奇怪的是在搜索中没有出现与此相关的任何内容。

谢谢。

I've heard that it's a good practice to define your records in PL/SQL by using the %ROWTYPE attribute. This saves typing and allows your package to continue functioning even when a column is added or deleted. (Correct me if I'm wrong!)

However, when I am fetching from a cursor that involves a join, I find that I have to fetch into a programmer-defined record that includes a (quite-possibly long) hand-written list of every column returned by the join.

So my question is:
Is it possible to fetch into nested records, or fetch into a list of records, or do something to avoid such an ugly kludge? Everything I've tried leads to an error about the record not matching what's being returned by the cursor.

Returning the result of a join using a cursor seems like such a common use-case to me that it's strange that nothing related to this comes up in a search.

Thank you.

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

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

发布评论

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

评论(2

岁月如刀 2024-07-20 19:27:24

您可以使用cursor%rowtype。

样本:

declare
cursor c_c is
select emp.*, dept.* -- use aliasses if columns have same name
from emp
,    dept; -- for sample no join condition

r_c c_c%rowtype; 

begin
  for r_c in c_c loop -- with for loop even the definition of r_c is not needed.
  ...
  end loop;
end;
/

You can user cursor%rowtype.

Sample:

declare
cursor c_c is
select emp.*, dept.* -- use aliasses if columns have same name
from emp
,    dept; -- for sample no join condition

r_c c_c%rowtype; 

begin
  for r_c in c_c loop -- with for loop even the definition of r_c is not needed.
  ...
  end loop;
end;
/
×眷恋的温暖 2024-07-20 19:27:24

为什么还要费心游标声明呢?

这是等价的。

begin
  for r_c in (select emp.*, dept.* from emp, dept) loop
  ...
  end loop;
end;

我在你的评论中看到你提到了这一点。 但我看到显式游标语法使用得太多,我认为展示它很重要。

Why even bother with the cursor declaration?

This is equivalent.

begin
  for r_c in (select emp.*, dept.* from emp, dept) loop
  ...
  end loop;
end;

I see in your comment you mention this. But I see the explicit cursor syntax used so much, i think it's important to show.

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