如果今天的日期在表ABC的开始日期和结束日期之间,我需要编写一个过程来更新表Xyz的列。
table ABC
fin_cycle start_date end_date account_class
----------------------------------------------------
F2018 27-05-2020 29-05-20 2003
table xyz
account_no account_class ac_no_dr
1234 2003 Y
当我今天运行该过程时,如果今天的日期在表ABC的开始日期和结束日期之间,则该过程会将ac_no_dr列更新为Y,否则它将更新该列为N。我已经准备好此框架。
Create Or Replace PROCEDURE pr_no_debit
Cursor c_Today(start_date, end_Date) is
Select Today from sttm_branch where today between start_Date and end_Date;
l_No_Debit_List ABC%ROW_TYPE;
Begin
For c_Today(l_No_Debit_List.start_Date,l_No_Debit_List.end_Date) Loop
Update XYZ set ac_no_DR='Y' where account_class=l_No_Debit_List.account_class;
End Loop;
-- At the end of the period Change No_Debit to 'N'
End pr_no_debit;
答案 0 :(得分:1)
这里是一个选项:merge
。 (今天是2020年5月27日,介于abc
表中存储的开始日期和结束日期之间。)
样本数据:
SQL> select * From abc;
FIN_C START_DATE END_DATE ACCOUNT_CLASS
----- ---------- ---------- -------------
F2018 27.05.2020 29.05.2020 2003
SQL> select * From xyz;
ACCOUNT_NO ACCOUNT_CLASS A
---------- ------------- -
1234 2003
合并语句:
SQL> merge into xyz a
2 using (select account_class,
3 case when sysdate between start_date and end_date then 'Y'
4 else 'N'
5 end ac_no_dr
6 from abc
7 ) x
8 on (a.account_class = x.account_class)
9 when matched then update set a.ac_no_dr = x.ac_no_dr;
1 row merged.
结果:
SQL> select * From xyz;
ACCOUNT_NO ACCOUNT_CLASS A
---------- ------------- -
1234 2003 Y
SQL>
最重要的是:您不需要过程或循环(效率低下),因为一切都可以用一个SQL语句完成。
如果-正如您所评论-必须是一个过程,那么也没问题:
create or replace procedure p_merge as
begin
merge into xyz a
using (select account_class,
case when sysdate between start_date and end_date then 'Y'
else 'N'
end ac_no_dr
from abc
) x
on (a.account_class = x.account_class)
when matched then update set a.ac_no_dr = x.ac_no_dr;
end;
/