2013-12-19 83 views
0

我必須爲以下要求編寫存儲過程。plsql插入,更新現有ID

所需的輸出:稅率表

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)2012/4/1插入,則默認值應放置在TO_DATE柱說31-DEC-9998。當我嘗試輸入相同的cat_id 1時,差異值設置爲tax_percentage 12.5,此時from_date 2013年4月1日,則to_date值應爲(from_date-1)cat_id 1,tax_percent 10和我的to_date值10,cat_id = 1應使用默認的日期更新31日 - 12月9998.something這樣

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

我想更新列2的TO_DATE列,即CATEGORY_ID 1 TAX_PERCENTAGE 12.5與默認日期值同時我想要更改col 1列的to_date列,即CATEGORY_ID 1 TAX_PERCENTAGE 10與列2 from_date -1 – user3099684

+0

任何幫助這是讚賞 – user3099684

回答

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;