oracle - 如何存储受影响记录的数量并将其作为参数返回?
问题描述
我有一个 in 参数(set = 0),用于跟踪我将要修改的条目数。我正在尝试将数据合并到一个名为 Table1 的表中,自上次更新 Table1 以来,这些记录已在另一个表 (Table2) 中更新。条件语句会将Table1.LastUpdate列与Table2的max(Modified_date ) 列进行比较,并且只插入table1.last_update列大于table2.max(modified_date)列的条目。然后我需要存储这个数字并将其作为输出参数返回。我所拥有的如下:
create or replace procedure test_proc (rUpdated_Row_Count IN NUMBER, rUpdated_Row_Count_2 OUT NUMBER) is
CURSOR c1 is
select max(modified_date) as max_modified_date
from table1;
l_var c1%ROWTYPE;
-----------
CURSOR c2 is
select table2_id
, last_update
from table2;
k_var c2%ROWTYPE;
BEGIN
LOOP
Open c1;
Fetch c1 into l_var;
Open c2;
Fetch c2 into k_var;
EXIT WHEN c1%NOTFOUND;
IF k_var.last_update > l_var.max_modified_date THEN
Insert into table2(table2_id, last_update)
values(null, k_var.last_update);
commit;
rUpdated_Row_Count_2 := rUpdated_Row_Count + 1;
END IF;
END LOOP;
Close c1;
Close c2;
END test_proc;
提前致谢!
修改了我的代码(经过进一步研究):
create or replace procedure test_proc (rUpdated_Row_Count IN NUMBER, rUpdated_Row_Count_2 OUT NUMBER) is
CURSOR c1 is
select max(modified_date) as max_modified_date
from table1;
l_var c1%ROWTYPE;
-----------
CURSOR c2 is
select table2_id
, last_update
from table2;
k_var c2%ROWTYPE;
BEGIN
Open c1;
Open c2;
LOOP
Fetch c1 into l_var;
Fetch c2 into k_var;
EXIT WHEN c2%NOTFOUND;
IF k_var.last_update > l_var.max_modified_date THEN
Insert into table2(table2_id, last_update)
values(null, k_var.last_update);
commit;
rUpdated_Row_Count_2 := rUpdated_Row_Count + 1;
END IF;
END LOOP;
Close c1;
Close c2;
END test_proc;
可重现的数据/代码如下:
Create table1
(
table1_id number,
modified_date date
);
Create table2
(
table2_id number,
last_update date
);
insert into table1(table1_id, modified_date) values(1, sysdate);
insert into table1(table1_id, modified_date) values(2, sysdate);
insert into table1(table1_id, modified_date) values(3, sysdate -1);
insert into table2(table2_id, last_update) values(1, sysdate + 1);
insert into table2(table2_id, last_update) values(2, sysdate + 2);
解决方案
不太确定“IN”参数的用途。也不太确定整体原理。但是,这是我编写程序的第一个版本的方式:
create or replace procedure test_proc2 (
rUpdated_Row_Count IN NUMBER
, rUpdated_Row_Count_2 IN OUT NUMBER )
is
max_modified_date date ;
begin
select max( modified_date ) into max_modified_date from table1 ;
for rec_ in (
select table2_id, last_update
from table2
) loop
if rec_.last_update > max_modified_date then
insert into table2( table2_id, last_update )
values( null, rec_.last_update ) ;
rUpdated_Row_Count_2 := rUpdated_Row_Count_2 + 1 ;
end if ;
end loop;
end ;
/
使用您的测试表(顺便说一下,您的 DDL 代码应该是:CREATE TABLE table1 ...),我们可以使用以下匿名块来执行该过程。
-- not sure what the "IN" parameter is used for
declare
rowcount_in number := 0 ; -- not needed
rowcount_out number := 0 ;
begin
test_proc2( rowcount_in, rowcount_out ) ;
dbms_output.put_line( 'updated rows: ' || rowcount_out ) ;
end;
/
updated rows: 2
执行匿名块后,表包含...
SQL> select * from table1 ;
TABLE1_ID MODIFIED_DATE
1 15-MAY-18
2 15-MAY-18
3 14-MAY-18
SQL> select * from table2 ;
TABLE2_ID LAST_UPDATE
1 16-MAY-18
2 17-MAY-18
NULL 16-MAY-18
NULL 17-MAY-18
许多人会告诉您,您应该尽可能使用 BULK 操作(BULK COLLECT、FORALL 等)。这一切对你有帮助吗?
推荐阅读
- three.js - 如何制作具有多种领型的 3d 模型衬衫?
- java - 如何通过 Windows 上内置的 tomcat 脚本文件启动和停止 tomcat?
- swift - UILabel 的内容大小不等于宽度锚点
- ruby-on-rails - SQL 注入预防使用带有方法 pluck 和 <<~INFO_SQL 的参数
- wordpress - Wordpress Newspaper Gallery 在 amp 版本上损坏,如何修复?
- python - 从函数中捕获异常
- ruby-on-rails - 如何显示从 Rails API 到 Nuxt 前端的应用程序/pdf 响应
- python - 如何忽略odoo商店链接中的额外参数
- javascript - Html Input 接受逗号或点作为小数位
- ms-word - microsoft word中自动粘贴后如何替换