DBMS_SQL.执行和批量更新 - 需要帮助

发布于 2024-10-19 01:53:06 字数 288 浏览 3 评论 0原文

我有这个查询

从表中选择 col1、col2,其中 标准 = :criteria_var

DBMS_SQL 中使用特定查询来打开游标并绑定变量。然后,DBMS_SQL 函数将获取这些值,然后更新另一个表。但这是一次一排发生的。

我想使用 BULK FETCH INTO。我已阅读教程,但找不到任何可以使用 DBMS_SQL 进行 BULK FETCH INTO 的内容。

是否可以?如果是,那么如何?

I have this query

select col1, col2 from table where
critera = :criteria_var

The particular query was being used in DBMS_SQL to open cursor and BIND the variables. The values are then being fetched by DBMS_SQL functions which are then updating another table. but this is happening one row at a time.

I want to use BULK FETCH INTO. I have read the tutorials but i couldn't find anythign where i can use BULK FETCH INTO with DBMS_SQL.

is it possible? if yes, then how?

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

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

发布评论

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

评论(1

我的奇迹 2024-10-26 01:53:06

您可以使用 DBMS_SQL 包中的 BIND_ARRAY 过程来执行批量获取。有一个示例 DBMS_SQL 文档。

然而,除非特别需要使用 DBMS_SQL(并且首先假设动态 SQL 实际上是必要的),否则使用本机动态 SQL 似乎会更容易,即

EXECUTE IMMEDIATE 'SELECT col1, col2 FROM tableName WHERE criteria = :1'
   BULK COLLECT INTO l_col1_collection, l_col2_collection
  USING l_criteria_variable;

如果您只是从此查询中获取数据然而,为了更新不同的表,让 Oracle 通过构建使用此查询获取多行的单个 UPDATE 语句来完成这项工作会更有效。像这样的东西

UPDATE destination_table dest
   SET (col1, col2) = (SELECT col1, col2
                         FROM source_table_name src
                        WHERE criteria = l_criteria_variable 
                          AND src.key_column = dest.key_column)
 WHERE EXISTS( SELECT 1
                 FROM source_table_name src
                WHERE criteria = l_criteria_variable 
                  AND src.key_column = dest.key_column)

You can use the BIND_ARRAY procedure in the DBMS_SQL package to do a bulk fetch. There is an example of this in the DBMS_SQL documentation.

Unless there is a particular need to use DBMS_SQL, however, (and assuming dynamic SQL is actually necessary in the first place) it seems likely that it would be easier to use native dynamic SQL, i.e.

EXECUTE IMMEDIATE 'SELECT col1, col2 FROM tableName WHERE criteria = :1'
   BULK COLLECT INTO l_col1_collection, l_col2_collection
  USING l_criteria_variable;

If you are just fetching the data from this query in order to update a different table, however, it would be more efficient to just let Oracle do that work by constructing a single UPDATE statement that used this query to fetch multiple rows. Something like

UPDATE destination_table dest
   SET (col1, col2) = (SELECT col1, col2
                         FROM source_table_name src
                        WHERE criteria = l_criteria_variable 
                          AND src.key_column = dest.key_column)
 WHERE EXISTS( SELECT 1
                 FROM source_table_name src
                WHERE criteria = l_criteria_variable 
                  AND src.key_column = dest.key_column)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文