仅当元素不存在时,如何将其插入到嵌套表中?

发布于 2024-12-08 11:16:36 字数 895 浏览 0 评论 0原文

我想要一个包含自定义对象的嵌套表,通过从多个游标中一一添加它们。但我不想在表中出现重复项。我怎样才能实现这个目标?

以下是我向表中添加元素的方法:

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 技术交流群。

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

发布评论

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

评论(1

忘羡 2024-12-15 11:16:36

如果您使用的是 Oracle 10g 或更高版本,有几种简单的方法可以解决此问题。

一种是使用现有代码,并在 OPERATORS 循环中使用 MEMBER OF 运算符来测试集合中是否已存在。

然而,更简单的方法是定义三个集合。使用批量收集用每个查询中的完整记录集填充两个集合,然后使用第三个集合过滤掉任何重复项。

像这样的事情:

declare 

    type recipients_list is table of recipient;
    recs recipients_list := recipients_list();
    admins recipients_list := recipients_list();
    operators recipients_list := recipients_list();

begin

    select firstname, lastname, email
    bulk collect into admins
    from users
    where profile_id = 1;

    select firstname, lastname, email
    bulk collect into operators
    from users
    where operator = 1;

    recs := admins multiset union distinct operators;

end;
/

“是否可以有多个联合?”

这肯定是有可能的。我不知道是否有上限,但如果有的话,那将会是一个很大的数字。我发现 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:

declare 

    type recipients_list is table of recipient;
    recs recipients_list := recipients_list();
    admins recipients_list := recipients_list();
    operators recipients_list := recipients_list();

begin

    select firstname, lastname, email
    bulk collect into admins
    from users
    where profile_id = 1;

    select firstname, lastname, email
    bulk collect into operators
    from users
    where operator = 1;

    recs := admins multiset union distinct operators;

end;
/

"Is it possible to have multiple unions? "

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.

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