2012-04-13 11 views
0

状況1に、2つの日時の間の時間差を取得します。オラクル

p_start_date = 13.Apr.2012 13:00 
p_end_date = 13.Apr.2012 15:00 

working_hours = 0 

// in this Situation working_hours should be 2 

状況2:私はSituation 1Situation 2SQLDevelopperでこれを実行すると

p_start_date = 13.Apr.2012 13:00 
p_end_date = 14.Apr.2012 15:00 

working_hours = 24 

// in this Situation working_hours should be 26 

は、working_hoursの値が正しく返しています。私の頂点アプリケーションでの使用のためにBefore Insert Update Triggerでこのプロシージャを呼び出します。 Situation 1の表形式を提出すると、working_hoursの値は0であり、Situation 2の表形式を提出すると、working_hoursの値は24となります。

Situation 1Situation 2によると、2つの日付の違いはworking_hoursに割り当てられます。しかし、私の必要性は2回の違いがあります。

どうすればいいですか?

working_hoursがある計算に私の手順、

PROCEDURE get_labour_cost_data(
p_employee_id IN NUMBER, 
p_start_date IN VARCHAR2, 
p_end_date IN VARCHAR2, 
hours_normal_rate OUT NUMBER, 
working_hours OUT NUMBER, 
total_cost OUT NUMBER) 

AS 

v_employee_rate NUMBER; 

BEGIN 

    if p_employee_id is null then 

    hours_normal_rate := 0; 
    working_hours := 0; 
    total_cost := 0; 

    elsif p_employee_id is not null then 

    -- Get hourse_noraml from employee 
    select HOURLY_SALARY into hours_normal_rate from Employee 
    where EMPLOYEE_ID = p_employee_id; 

    -- Get working hours 
    working_hours := 24 * (to_date(p_end_date, 'dd.mm.rr hh24:mi') - to_date(p_start_date, 'dd.mm.rr hh24:mi')); 

    -- Get Total cost 
    total_cost := nvl(hours_normal_rate,0) * nvl(working_hours,0); 

    end if;  
    END; 

トリガーがあり、

私が作成し、それが動作するだけで、あなたの手順を実行し
create or replace 
TRIGGER LABOUR_COST_BIU_TRI 
BEFORE INSERT OR UPDATE ON LABOUR_COST 
FOR EACH ROW 
DECLARE 

v_hours_normal NUMBER; 
v_working_hours NUMBER; 
v_total_cost NUMBER; 
BEGIN 

    util.get_labour_cost_data(
    p_employee_id => :NEW.EMPLOYEE_ID, 
    p_start_date => :NEW.START_DATE_TIME, 
    p_end_date => :NEW.END_DATE_TIME, 
    hours_normal_rate => v_hours_normal, 
    working_hours => v_working_hours, 
    total_cost => v_total_cost 
); 

     select v_hours_normal, v_working_hours, v_total_cost into :NEW.HOURS_NOMAL, :NEW.HOURS_OT, :NEW.TOTAL_COST 
    from dual; 

END; 

答えて

0

declare 
hours_normal_rate NUMBER; 
working_hours NUMBER; 
total_cost NUMBER; 
begin 
get_labour_cost_data(
1, 
'13.Apr.2012 13:00', 
'13.Apr.2012 15:00', 
hours_normal_rate, 
working_hours, 
total_cost); 
dbms_output.put_Line(working_hours); 
END; 

出力:

2 

そして:

declare 
hours_normal_rate NUMBER; 
working_hours NUMBER; 
total_cost NUMBER; 
begin 
get_labour_cost_data(
1, 
'13.Apr.2012 13:00', 
'14.Apr.2012 15:00', 
hours_normal_rate, 
working_hours, 
total_cost); 
dbms_output.put_Line(working_hours); 
END; 

出力:

25.99999999999999999999999999999999999992 

(!閉じます)

それにもかかわらず私は、あなたが引き金にそれを使用する方法に問題を見ることができません次のように簡略化することができます。

create or replace 
TRIGGER LABOUR_COST_BIU_TRI 
BEFORE INSERT OR UPDATE ON LABOUR_COST 
FOR EACH ROW 
BEGIN 

    util.get_labour_cost_data(
    p_employee_id => :NEW.EMPLOYEE_ID, 
    p_start_date => :NEW.START_DATE_TIME, 
    p_end_date => :NEW.END_DATE_TIME, 
    hours_normal_rate => :NEW.HOURS_NOMAL, 
    working_hours => :NEW.HOURS_OT, 
    total_cost => :NEW.TOTAL_COST 
); 

END;