仅当元素不存在时,如何将其插入到嵌套表中?
我想要一个包含自定义对象的嵌套表,通过从多个游标中一一添加它们。但我不想在表中出现重复项。我怎样才能实现这个目标?
以下是我向表中添加元素的方法:
create type recipient as object (firstname varchar2, lastname varchar2, email varchar2);
declare type recipients_list is table of recipient;
rec recipients_list := recipients_list();
cursor admins is
select firstname, lastname, email
from users
where profile_id = 1;
cursor operators is
select firstname, lastname, email
from users
where operator = 1;
-- an user may be both admin and operator
....
for to_email in admins
loop
rec.extend;
rec(rec.last) := recipient(to_email.firstname, to_email.lastname, to_email.email);
end loop;
for to_email in operators
loop
rec.extend;
rec(rec.last) := recipient(to_email.firstname, to_email.lastname, to_email.email);
end loop;
请注意,这只是一个示例,在这种特殊情况下,可以更改选择以便只有一个光标。但在我的应用程序中情况有所不同。谢谢!
I want to have a nested table holding custom objects, by adding them one by one from multiple cursors. But I don't want to have duplicates in the table. How can I achieve this?
Here is how I add elements to the table:
create type recipient as object (firstname varchar2, lastname varchar2, email varchar2);
declare type recipients_list is table of recipient;
rec recipients_list := recipients_list();
cursor admins is
select firstname, lastname, email
from users
where profile_id = 1;
cursor operators is
select firstname, lastname, email
from users
where operator = 1;
-- an user may be both admin and operator
....
for to_email in admins
loop
rec.extend;
rec(rec.last) := recipient(to_email.firstname, to_email.lastname, to_email.email);
end loop;
for to_email in operators
loop
rec.extend;
rec(rec.last) := recipient(to_email.firstname, to_email.lastname, to_email.email);
end loop;
Note that this is just an example, and in this particular case the selects could be changed in order to have just one cursor. But in my application things are different. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用的是 Oracle 10g 或更高版本,有几种简单的方法可以解决此问题。
一种是使用现有代码,并在 OPERATORS 循环中使用 MEMBER OF 运算符来测试集合中是否已存在。
然而,更简单的方法是定义三个集合。使用批量收集用每个查询中的完整记录集填充两个集合,然后使用第三个集合过滤掉任何重复项。
像这样的事情:
这肯定是有可能的。我不知道是否有上限,但如果有的话,那将会是一个很大的数字。我发现 Oracle 中的限制往往比较慷慨;如果我们发现自己正在突破极限,那么无论做什么,都可能有更好的方法。
If you are using Oracle 10g or higher there are a couple of easy ways to solve this.
One would be to use your existing code, and in the OPERATORS loop use the MEMBER OF operator to test whether it already exists in the collection.
However, a simpler approach would be to define three collections. Use bulk collect to populate two collections with the complete set of records from each query, and then use the third collection to filter out any duplicates.
Soemthing like this:
It certain is possible. I don't know whether there is a maximum but if there is it will be a high number. I have found that limits in Oracle tend to be on the generous side; if we find ourselves butting against the limits there's probably a better way of doing whatever it is.