我必须为以下要求编写存储过程。
所需的输出:税表
CATEGORY_ID TAX_PERCENTAGE FROM_DATE CREATE_DATE TAX_ID TO_DATE
-------------------------------------------------------------------------------------
1 10 4/1/2012 12/19/2013 8:54:20 PM 61 31-MAR-13
1 12.5 4/1/2013 12/19/2013 8:54:44 PM 62 31-dec-9998
当插入新的CATEGORY_ID 1表示工作年度为tax_percentage 10(from_date)4/1/2012时,应将默认值放在to_date列中,例如31-dec-9998。当我尝试输入相同的cat_id 1并使用diff值设置为tax_percentage 12.5时,这次from_date 4/1/2013然后to_date值应该是(from_date-1)cat_id 1,tax_percent 10和my to_date值10,cat_id = 1应该使用默认日期31-dec-9998更新。像这样的东西
CREATE OR REPLACE procedure SCOTT.Sp_SaveNewTaxPercentage
(
iv_category_id number,
iv_tax_percentage varchar2,
iv_from_date varchar2,
iv_to_date varchar2 default '12/31/9998' ,
iv_created_date date,
ov_err_code out nocopy varchar2,
ov_err_msg out nocopy varchar2
)
is
lv_category_id varchar2(25);
LV_TO_DATE varchar2(25);
lv_cat_id varchar2(12);
begin
ov_err_code:=0;
for J in ( SELECT CATEGORY_ID,FROM_DATE FROM TAX_P WHERE CATEGORY_ID =IV_CATEGORY_ID)
loop
SELECT to_date(iv_from_date,'dd/mm/yyyy') -1 INTO LV_TO_DATE FROM DUAL;
lv_cat_id := J.CATEGORY_ID;
end loop;
update tax_p set TO_DATE=LV_TO_DATE where CATEGORY_ID =lv_cat_id;
commit;
IF lv_cat_id IS NULL THEN
LV_TO_DATE := iv_to_Date;
END IF;
insert into tax_p( TAX_ID , CATEGORY_ID ,TAX_PERCENTAGE, FROM_DATE ,TO_DATE,CREATE_DATE)
values(tax_seq.nextval,iv_category_id,iv_tax_percentage,iv_from_date,LV_TO_DATE,sysdate);
select 'Successfully Saved' into ov_err_msg from dual;
commit;
Exception
when others then
rollback;
ov_err_code:=1;
ov_err_msg:='Error while saving'||SQLERRM;
end Sp_SaveNewTaxPercentage;
/
show errors;
答案 0 :(得分:0)
我认为您不需要循环记录来更新to_date
字段,只需一个UPDATE
语句就可以使用。
BEGIN
UPDATE tax_p
SET to_date = to_date(iv_from_date,'dd/mm/yyyy') -1
WHERE category_id = iv_category_id;
INSERT INTO tax_p(tax_id, category_id, tax_percentage, from_date, to_date, create_date)
VALUES(tax_seq.nextval, iv_category_id, iv_tax_percentage, iv_from_date, iv_to_date, SYSDATE);
COMMIT;
EXCEPTION WHEN ...
...
END;