我正在尝试编写一个程序,将paid_invoices
表中的已付款发票插入invoice_archive
表中。仅应传送早于或等于2014年5月31日的已付款发票。
这是我的程序:
SQL> create or replace procedure paid_invoice_transfer as
cursor paid is
select *
from paid_invoices
where invoice_total = credit_total + payment_total
and payment_date <= '2014-05-31';
invoice_archive_text paid%rowtype;
begin
for invoice_archive_text in paid loop
dbms_output.put_line(invoice_archive_text.invoice_id);
insert into invoice_archive values invoice_archive_text;
end loop;
end;
/
我不确定现在要执行什么:
SQL> set serveroutput on;
SQL> execute paid_invoice_transfer(???);
答案 0 :(得分:0)
现在,您知道如何不带参数地执行过程了,我也想指出一下过程中的问题。
不需要定义用于遍历游标的记录变量。
invoice_archive_text paid%rowtype
当在for循环中使用 Pl / SQL时,它会自动创建它。我将更进一步,请您避免运行INSERT
的循环。只需使用普通的INSERT INTO target_table select * from source_table
并明确指定列名即可。
如果要显示通过DBMS_OUTPUT.PUT_LINE
插入的ID,请将其存储在一个集合中,并单独循环以仅用于显示目的(仅当您认为需要显示时)。
我还想向您展示如何传递日期参数来执行,因此我将传递payment_date。在您的过程中,您错误地将其与文字字符串而不是日期进行比较。如果NLS_DATE_
参数与字符串不匹配,则注定会失败。
CREATE OR REPLACE PROCEDURE paid_invoice_transfer ( p_payment_date DATE ) AS
TYPE tab_inv IS TABLE OF paid_invoices.invoice_id%type;
t_tab tab_inv;
BEGIN
SELECT invoice_id BULK COLLECT INTO t_tab
FROM paid_invoices
WHERE invoice_total = credit_total + payment_total
AND payment_date <= p_payment_date;
FOR i IN t_tab.FIRST..t_tab.LAST LOOP
dbms_output.put_line(t_tab(i));
END LOOP;
INSERT INTO invoice_archive ( column1,column2,column3) select col1,col2,col3
FROM paid_invoices
WHERE invoice_total = credit_total + payment_total
AND payment_date <= p_payment_date;
END;
/
set serveroutput on
execute paid_invoice_transfer(DATE '2014-05-31' )