0

I am trying to create a trigger that would check for a null value in column namesdreservation_status from my table passenger and set it to a default value if it's null and show the user a message of it being set to default.

The trigger is created and works fine for setting the default but no message is shown to the user.

create or replace trigger mytrigger
    before insert or update on passenger
    for each row
    when (new.reservation_status IS NULL)
begin
    IF :new.reservation_status IS NULL THEN
        :new.reservation_status := 'not reserved';
        dbms_output.put_line('reservation status invalid, set to default');
    end IF;
end mytrigger
/

The value gets changed to 'not reserved' if its null but the message 'reservation status invalid, set to default' isn't displayed. Help.

2
  • are you running from sqlplus? You need to execute "set serveroutput on" Commented Nov 7, 2019 at 13:22
  • yes, i am using sqlplus. I did set serveroutput to on outside the trigger it did not do anything. Commented Nov 7, 2019 at 13:26

1 Answer 1

2

Trigger's don't do anything until they're actually triggered - in this case, on an INSERT.

clear screen
set serveroutput on

drop table passenger;
create table passenger (id integer, reservation_status varchar2(25));

create or replace trigger mytrigger
    before insert or update on passenger
    for each row
    when (new.reservation_status IS NULL)
begin
    IF :new.reservation_status IS NULL THEN
        :new.reservation_status := 'not reserved';
        dbms_output.put_line('reservation status invalid, set to default');
    end IF;
end mytrigger;
/

insert into passenger values (1, null);
commit;

If I run this in SQLPlus, SQLcl, or SQL Developer -

enter image description here

And if we check the table, you can see our trigger, 'worked.'

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.