在游标内使用 Merge 语句

发布于 2024-12-19 01:26:18 字数 238 浏览 1 评论 0原文

我们需要填充一个主表,该表由 20 个不同表中的列组成。

我编写了一个存储过程来连接一些返回最大列数的表并将它们放在游标中。

现在。我正在使用 for 循环来迭代游标记录,以便可以将它们插入主表中。

如何在游标 for 循环内使用合并语句,以便我可以检查是否需要更新现有行或插入新行,具体取决于记录是否已存在。

如果我们可以在游标 for 循环中使用 merge 语句,有什么想法吗?有什么例子吗?

We have a requirement to populate a master table which consists of columns from a set of 20 different tables.

I have written a stored procedure to join some of the tables that return me max number of columns and have them in a cursor.

Now. I am using for loop to iterate through the cursor records so I can insert them into the master table.

How I can use a merge statement inside the cursor for loop so I can check if I need to update existing row or insert a new row depending if the records already exists or not.

Any ideas if we can use merge statement inside a cursor for loop? Any examples?

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

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

发布评论

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

评论(1

云裳 2024-12-26 01:26:18

您可以通过从DUAL中选择光标的数据来执行MERGE。例如,

使用一些数据创建源表和目标表

SQL> create table src ( col1 number, col2 varchar2(10) );

Table created.

SQL> create table dest( col1 number, col2 varchar2(10) );

Table created.

SQL> insert into src values( 1, 'A' );

1 row created.

SQL> insert into src values( 2, 'B' );

1 row created.

SQL> insert into dest values( 1, 'C' );

1 row created.

SQL> commit;

Commit complete.

运行合并

SQL> ed
Wrote file afiedt.buf

  1  begin
  2    for x in (select * from src)
  3    loop
  4      merge into dest
  5      using( select x.col1 col1, x.col2 col2
  6               from dual ) src
  7         on( src.col1 = dest.col1 )
  8       when matched then
  9         update set col2 = src.col2
 10       when not matched then
 11         insert( col1, col2 )
 12           values( src.col1, src.col2 );
 13    end loop;
 14* end;
SQL> /

PL/SQL procedure successfully completed.

并验证合并是否符合我们的要求。第 1 行已更新,第 2 行已插入。

SQL> select * from dest;

      COL1 COL2
---------- ----------
         1 A
         2 B

然而,以这种方式构建代码通常没有太大意义。通常,最好将用于打开游标的查询直接放入 MERGE 语句中,这样您就可以选择所有数据,而不是从 DUAL 中选择一行数据您想要合并您尝试合并数据的所有表。当然,为该查询创建一个 MERGE 语句可以查询的视图以保持 MERGE 语句的可读性可能是有意义的。

You can do a MERGE by selecting the cursor's data from DUAL. For example

Create a source and destination table with some data

SQL> create table src ( col1 number, col2 varchar2(10) );

Table created.

SQL> create table dest( col1 number, col2 varchar2(10) );

Table created.

SQL> insert into src values( 1, 'A' );

1 row created.

SQL> insert into src values( 2, 'B' );

1 row created.

SQL> insert into dest values( 1, 'C' );

1 row created.

SQL> commit;

Commit complete.

Run the merge

SQL> ed
Wrote file afiedt.buf

  1  begin
  2    for x in (select * from src)
  3    loop
  4      merge into dest
  5      using( select x.col1 col1, x.col2 col2
  6               from dual ) src
  7         on( src.col1 = dest.col1 )
  8       when matched then
  9         update set col2 = src.col2
 10       when not matched then
 11         insert( col1, col2 )
 12           values( src.col1, src.col2 );
 13    end loop;
 14* end;
SQL> /

PL/SQL procedure successfully completed.

And verify that the merge did what we wanted. Row 1 was updated and row 2 was inserted.

SQL> select * from dest;

      COL1 COL2
---------- ----------
         1 A
         2 B

However, it generally wouldn't make too much sense to structure the code this way. You'd generally be better off putting the query that you'd use to open the cursor into the MERGE statement directly so that rather than selecting one row of data from DUAL, you're selecting all the data you want to merge from all the tables you're trying to merge the data from. Of course, it may make sense to create a view for this query that the MERGE statement can query in order to keep the MERGE statement readable.

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