pl sql trigger how to compare column value in trigger - oracle

I would like to create a ORACLE plsql trigger in my booking table which will prevent user to update value where booking start date like <= sysdate.
in my booking table booking start date column name is date_from. I have tried following command but seems like I am using wrong code.
create or replace TRIGGER UPDATE_BOOKING
BEFORE UPDATE ON BOOKING
FOR EACH ROW
BEGIN
IF BOOKING.DATE_FROM <= TO_DATE(SYSDATE)
THEN
RAISE_APPLICATION_ERROR (-20001,'YOU CAN NOT UPDATE BOOKING STATUS WHERE ......');
END IF;
END;
what would be the best way to implement this trigger.

If you want to compare the existing value of the date then use :old. If you are updating the date column also and you want check it for the new value of update date then use :new. Example for :old is as follows..
create or replace TRIGGER UPDATE_BOOKING
BEFORE UPDATE ON BOOKING
FOR EACH ROW
BEGIN
IF :OLD.DATE_FROM <= TO_DATE(SYSDATE)
THEN
RAISE_APPLICATION_ERROR (-20001,'YOU CAN NOT UPDATE BOOKING STATUS WHERE ......');
END IF;
END;

No need to use to_date() function for sysdate, its already date type, check the following code:
create or replace TRIGGER UPDATE_BOOKING
BEFORE UPDATE ON BOOKING
FOR EACH ROW
BEGIN
-- used trunc function below for more accuracy
IF trunc(:OLD.DATE_FROM) <= trunc(SYSDATE)
THEN
RAISE_APPLICATION_ERROR (-20001,'YOU CAN NOT UPDATE BOOKING STATUS WHERE ......');
END IF;
END;

Related

create a trigger in oracle to avoid insertion on sunday

I have employee table in oracle.I want to create a trigger that will avoid insertion of data on sun day. please tell me the program?following program not working.
Here's an example trigger that checks for week day and, if day is sunday (7), throws an user defined exception ORA-20000:
CREATE OR REPLACE TRIGGER trg_sunday
BEFORE INSERT ON employee
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
BEGIN
IF TO_CHAR(SYSDATE, 'D') = '7' THEN
RAISE_APPLICATION_ERROR(-20000, 'Cannot insert record on sunday');
END IF;
END;
Please check which number identifies sunday, it depends on the local NLS settings of the database. Here in Italy (NLS_TERRITORY='ITALY') Sunday is identified by 7 but with different database NLS setting the number may vary.
You may take a look at Day of week (1-7) and NLS settings
The trigger remains the same but the query goes like below:
to check whats day today"
select to_char(to_date('08/08/2016','dd/mm/yyyy'), 'DY'),TO_CHAR(SYSDATE, 'D') from dual;
To check next sunday:
select to_char(to_date('14/08/2016','dd/mm/yyyy'), 'DY'),TO_CHAR(to_date('14/08/2016','dd/mm/yyyy'), 'D') from dual;
The correct code should be:
CREATE OR REPLACE TRIGGER trg_sunday
BEFORE INSERT ON employee
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
BEGIN
IF TO_CHAR(SYSDATE, 'D') = '1' THEN ---Should be 1 rather than 7
RAISE_APPLICATION_ERROR(-20000, 'Cannot insert record on sunday');
END IF;
END;
You can create your trigger as shown in below code snippet.
CREATE OR REPLACE TRIGGER sunday_prevent
BEFORE INSERT ON account
FOR EACH ROW
Begin
IF TO_CHAR(SYSDATE,'day')='sunday' then
raise_application_error(-20000,'Today is Sunday so you not perform any transaction');
END IF;
END;
/

How to create trigger in oracle database so that future date cannot set?

I have a column 'patient_dob' in 'patient' table. Now I want to create a trigger in oracle database so that patient cannot set future date as their birth date. So I ave write the following query:
create or replace trigger test_trigger
before insert
on employees
for each row
BEGIN
IF patient_dob > sysdate
THEN
RAISE_APPLICATION_ERROR(-20950, 'date error');
END IF;
END;
but the code is not working. Can anyone tell me how to create a trigger to perform the above functionality?
Thanks
Try using new to identify the column:
create or replace trigger test_trigger
before insert
on employees
for each row
BEGIN
IF :new.patient_dob > sysdate THEN
-------^
RAISE_APPLICATION_ERROR(-20950, 'date error');
END IF;
END;

Using a trigger to implement a foreign key check constraint

I'm trying to implement this constraint into my database:
(In the table Race)
CONSTRAINT (Date <= Meeting.EndDate)
Essentially the column in the Race Table needs to be Less than the EndDate column in the Meeting table.
Pretty sure I need to use a trigger to implement a foreign key however, I'm not that sure how I would go about implementing one. So Far, All I have is:
CREATE OR REPLACE TRIGGER RaceDateCheck
AFTER UPDATE OF Race ON StartDateCheck
BEGIN
INSERT INTO Race
(MeetingEndDate)
SELECT EndDate FROM Meeting
END;
Thanks,
Presumably the race date has to be between the meeting start and end date, so you could check both at once; and also presumably you want to check this for new records, not just updates. So you could use something like:
CREATE OR REPLACE TRIGGER RaceDateCheck
BEFORE INSERT OR UPDATE ON Race
FOR EACH ROW
DECLARE
meetingStart Meeting.MeetingStartDate%TYPE;
meetingEnd Meeting.MeetingEndDate%TYPE;
BEGIN
SELECT StartDate, EndDate
INTO meetingStart, meetingEnd
FROM Meeting
WHERE MeetingID = :NEW.MeetingID;
IF :NEW.RaceDate < meetingStart
OR :NEW.RaceDate > meetingEnd THEN
RAISE_APPLICATION_ERROR(-20001, 'Invalid race date');
END IF;
END;
For just the end date:
CREATE OR REPLACE TRIGGER RaceDateCheck
BEFORE INSERT OR UPDATE ON Race
FOR EACH ROW
DECLARE
meetingEnd Meeting.MeetingEndDate%TYPE;
BEGIN
SELECT EndDate
INTO meetingEnd
FROM Meeting
WHERE MeetingID = :NEW.MeetingID;
IF :NEW.RaceDate > meetingEnd THEN
RAISE_APPLICATION_ERROR(-20001, 'Invalid race date');
END IF;
END;

fire trigger after insert in oracle

I'm very new for trigger, now this what i was trying. I've two tables INSERTED and ORDER_INFO, both have the same column name ORDER_ID, ORDER_DATE. I've scenario, where client will be placing his/her order then, order information will be stored into INSERTED table, then by using this trigger, it'll insert into another table ORDER_INFO after satisfying the condition, which has been written.
create trigger tri_check
AFTER INSERT ON inserted FOR EACH ROW
DECLARE
v_date DATE;
BEGIN
SELECT order_date INTO v_date FROM inserted;
if (v_date)< (sysdate + 2) then
raiserror('You cannot take an order to be delivered less than 2 days from now',16, 1);
else
INSERT INTO orders_info
( order_id,order_date)
VALUES
(:new.order_id,v_date);
end if;
end;
But, when i'm executing the above trigger, then i'm getting this error.
ERROR at line 8: PL/SQL: SQL Statement ignored
6. SELECT order_date INTO v_date FROM inserted;
7. if (v_date)< (sysdate + 2) then
8. raiserror('You cannot take an order to be delivered less than 2 days from now',16, 1);
9. else
10. INSERT INTO orders_info
EDIT
Now, i made the same structure table into SYSTEM user, and got the same error. Table or View does not exist
Need help !! Thanks in advance !!
The message seems to indicate a problem with the 'raiserror' procedure. I'm not familiar with such a procedure in standard PL/SQL - did you mean RAISE_APPLICATION_ERROR? However, and perhaps more to the point, when using a trigger there's no need to do a SELECT from the table. All the data being inserted is available to the trigger. I suggest changing your trigger to be something like the following:
create trigger tri_check
AFTER INSERT ON inserted
REFERENCING NEW AS NEW
FOR EACH ROW
BEGIN
if :new.ORDER_DATE < sysdate + INTERVAL '2' DAY then
RAISE_APPLICATION_ERROR(-20000, 'You cannot take an order to be delivered less than 2 days from now');
else
INSERT INTO orders_info
(order_id, order_date)
VALUES
(:new.order_id, :new.ORDER_DATE);
end if;
end TRI_CHECK;
Share and enjoy.
You can just use the :NEW and :OLD values instead of your select:
CREATE TRIGGER tri_check
AFTER INSERT
ON inserted
FOR EACH ROW
DECLARE
BEGIN
IF :new.order_date < (SYSDATE + 2)
THEN
raiserror (
'You cannot take an order to be delivered less than 2 days from now',
16,
1);
ELSE
INSERT INTO orders_info (order_id, order_date)
VALUES (:new.order_id, :new.order_date);
END IF;
END;
What is your raiserror procedure? Do you have access permissions granted on it?
Hope it helps...
EDIT:
OK, from your error, and the error you posted on #Bob Jarvis' answer, you might not have INSERT privilege on the ORDERS_INFO table. You also should check your permissions on the INSERTED table too.
Check your permissions with your DBA.
If raiserror is not a defined procedure or you don't have access to it then use the RAISE_APPLICATION_ERROR method for raising an error as Bob suggests.

How to set new date entries to current date?

I am trying to create a trigger. It is supposed to update any new date entry to sysdate. So far, I have the following code. However, I get "invalid table name" and "SQL statement ignored" errors.
CREATE OR REPLACE TRIGGER new_orders
AFTER INSERT ON orders
FOR EACH ROW
BEGIN
IF INSERTING THEN
UPDATE
SET order_date := SYSDATE;
END IF;
END;
/
CREATE OR REPLACE TRIGGER new_orders
BEFORE INSERT ON orders
FOR EACH ROW
BEGIN
:NEW.order_date := SYSDATE;
END;
/

Resources