Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Events based on sysdate [duplicate]

Possible Duplicate:
Trigger based on sysdate

1.I have a table on which i have to perform update operations everyday at 12:00(24 Hr. Format).

How should I accomplish this?

Table Schema:

CREATE TABLE CHARGES   
(
    total NUMBER(30),
    admitdate TIMESTAMP(6),
    dischargedate TIMESTAMP(30)
)

Update Algorithm:

if 
{
   dischargedate="null" 
   then total=admitdate-sysdate=difference in days * Total
   Do this every day at 12:00(24 Hr. Format)
}
else
{  
  do nothing.    
}
like image 231
Akki Avatar asked Nov 13 '22 14:11

Akki


1 Answers

The standard to run a job every 24 hours would be to run a job at this interval using the system package DBMS_SCHEDULER.

For instance:

BEGIN
  DBMS_SCHEDULER.create_job (
    job_name        => 'update_charges',
    job_type        => 'PLSQL_BLOCK',
    job_action      => 'BEGIN my_procedure; END;',
    start_date      => TRUNC(SYSDATE) + 0.5,
    repeat_interval => 'freq=daily;',
    end_date        => NULL,
    enabled         => TRUE,
    comments        => 'Update the discharged date in charges.');
END;
/

You then create a procedure to run:

create or replace PROCEDURE my_procedure is

begin

  update charges
     set total = admitdate - sysdate
   where dischargedate is null;

end;
/

This would update the column total to be the number of days between the admitdate and SYSDATE.

However, I question the need to do this at all. It sounds very much like the age-old "Should I store Age" question. I believe the answer is no. You are absolutely bound to be wrong at some point and there are a number of possibilities that might cause the job to be manually run incorrectly. I would calculate this column on the fly as you extract data from the database.

like image 179
Ben Avatar answered Nov 15 '22 07:11

Ben