获取Oracle中两个日期时间之间的时差

时间:2012-04-13 06:57:31

标签: stored-procedures triggers oracle10g oracle-apex

情况1:

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:

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

当我使用SQLDevelopperSituation 1Situation 2上运行时,working_hours的值会正确返回。但我在Before Insert Update Trigger中称这个程序用于我的顶点应用程序。当我使用Situation 1提交表格形式时,working_hours的值为0,当我使用Situation 2提交表格形式时,working_hours的值为{{1} }。

根据24Situation 1,两个日期之间的差异分配给working_hours。但我需要两次之间的区别。

我怎么能这样做?

我的计算Situation 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;

1 个答案:

答案 0 :(得分: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;