oracle PL/SQL:从游标中获取 2 个或更多连接表的最佳实践?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/589253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PL/SQL: Best practice for fetching 2 or more joined tables from a cursor?
提问by Edwin
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!)
我听说使用 %ROWTYPE 属性在 PL/SQL 中定义记录是一种很好的做法。这可以节省输入并允许您的包继续运行,即使添加或删除列也是如此。(如果我错了纠正我!)
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 somethingto 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.
谢谢你。
回答by Edwin
You can user cursor%rowtype.
您可以使用 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;
/
回答by Edwin
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.
我在您的评论中看到您提到了这一点。但是我看到显式游标语法使用得如此之多,我认为展示它很重要。