简体   繁体   中英

Call a stored procedure in a trigger? SQL

CREATE OR REPLACE PROCEDURE TIME_CHECK
as

JOUR date;
HEURE date;
BEGIN

select to_char(sysdate, 'DAY'), to_char(sysdate,'HH.MI.SS')
into jour, heure
FROM DUAL   
WHERE JOUR = to_char(sysdate, 'DAY') AND HEURE = to_char(sysdate,'HH.MI.SS');

if (jour not between 'SUNDAY' AND 'FRIDAY') and (heure not between '08.00.00' AND '22.00.00')
or (jour !='SATURDAY') AND (HEURE NOT BETWEEN '08.00.00' AND '12.00.00')

then raise_application_error(-20111,'OUT OF BUSINESS HOURS');

end if; 

end;

-- TRIGGER

CREATE OR REPLACE TRIGGER mod_job_history

before insert or update  on JOB_HISTORY

for each row

BEGIN

CALL TIME_CHECK 
---??????
        
END mod_job_history;

My procedure is supposed to valid if the user is trying to do something on business hours or not and the trigger is to be sure that the user is not allowed to insert or update the table job_history on business hours?

I've been working on it for several hours and I cannot find what's wrong help plz and how should I call my procedure via my trigger

thank you

Just use TIME_CHECK; :

CREATE TRIGGER mod_job_history
before insert or update  on JOB_HISTORY
for each row
BEGIN
  TIME_CHECK;
END mod_job_history;
/

Also, your procedure will not work as intended as JOUR is initially set to NULL and the you are comparing against it in the WHERE clause.

You can use the procedure below (which will work regardless of which language you use):

CREATE PROCEDURE TIME_CHECK
AS
  day  PLS_INTEGER := TRUNC(SYSDATE) - TRUNC(SYSDATE, 'IW');
  time INTERVAL DAY TO SECOND := (SYSDATE - TRUNC(SYSDATE)) DAY TO SECOND;
BEGIN
  IF ( day IN (0, 1, 2, 3, 4) -- Monday to Friday
     AND time BETWEEN INTERVAL '8' HOUR
                  AND INTERVAL '22' HOUR
     )
     OR
     ( day = 5 -- Saturday
     AND time BETWEEN INTERVAL '08' HOUR
                  AND INTERVAL '12' HOUR
     )
  THEN
    -- Inside business hours
    NULL;
  ELSE
    raise_application_error(-20111,'OUT OF BUSINESS HOURS');
  END IF;
END;
/

db<>fiddle here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM