如何存储受影响的记录数并将其作为参数返回?

时间:2018-05-15 17:54:25

标签: oracle plsql

我有一个in参数(set = 0),用于跟踪我将要修改的条目数。我正在尝试将数据合并到一个名为Table1的表中,这是自上次更新Table1以来已在不同表(Table2)中更新的记录。条件语句会将 Table1.LastUpdate 列与 Table2 max(Modified_date )列进行比较,并仅插入 table1的条目.last_update 列大于 table2.max(modified_date)列。然后我需要存储这个数字并将其作为out参数返回。我所拥有的是:

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);

1 个答案:

答案 0 :(得分: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等)。这一切对你有帮助吗?