Oracle Triggers Update at Another Table

给你一囗甜甜゛ 提交于 2019-12-24 11:09:21

问题


I am trying to create a trigger in Oracle. I know sql but i have never created trigger before. I have this code:

create or replace trigger "PASSENGER_BOOKING_T1"
AFTER
insert on "PASSENGER_BOOKING"
for each row
begin
IF (:NEW.CLASS_TYPE == 'ECO')

SELECT F.AVL_SEATS_ECOCLASS,F.FLIGHT_ID INTO SEAT, FLIGHT_INFO 
FROM BOOKING B, JOURNEY_FLIGHT J, FLIGHT F 
WHERE B.JOURNEY_ID = J.JOURNEY_ID and F.FLIGHT_ID = J.FLIGHT_ID;

UPDATE FLIGHT 
SET AVL_SEATS_ECOCLASS = (SEAT-1)
WHERE FLIGHT_ID = FLIGHT_INFO;

END IF;
end;​

This trigger fires when there is an insert in Passenger_Booking table. And seating capacity is reduced by one (which is at different table).

Select query should be alright but there is something wrong in somewhere. Could anyone suggest anything?

I changed the body part to this but still having issues:

UPDATE FLIGHT 
SET AVL_SEATS_ECOCLASS =
  (SELECT F.AVL_SEATS_ECOCLASS FROM BOOKING B, JOURNEY_FLIGHT J, FLIGHT F WHERE B.JOURNEY_ID = J.JOURNEY_ID and F.FLIGHT_ID = J.FLIGHT_ID;
);

回答1:


  1. An IF statement needs a THEN
  2. In PL/SQL, you use an = to test for equality, not ==
  3. You need to declare the variables that you are selecting into

When I do those three things, I get something like this

create or replace trigger PASSENGER_BOOKING_T1
  AFTER insert on PASSENGER_BOOKING
  for each row
declare
  l_seat       flight.seat%type;
  l_flight_id  flight.flight_id%type;
begin
  IF (:NEW.CLASS_TYPE = 'ECO')
  THEN
    SELECT F.AVL_SEATS_ECOCLASS,F.FLIGHT_ID 
      INTO l_seat, l_flight_id
      FROM BOOKING B, 
           JOURNEY_FLIGHT J, 
           FLIGHT F 
     WHERE B.JOURNEY_ID = J.JOURNEY_ID 
       and F.FLIGHT_ID = J.FLIGHT_ID;

    UPDATE FLIGHT 
       SET AVL_SEATS_ECOCLASS = (l_seat-1)
     WHERE FLIGHT_ID = l_flight_id;   
  END IF;
end;​

Beyond those syntax errors, I would be shocked if the SELECT INTO statement was correct. A SELECT INTO must return exactly 1 row. Your query should almost certainly return multiple rows since there are no predicates that would restrict the query to a particular flight or a particular booking. Presumably, you want to join to one or more columns in the PASSENGER_BOOKING table.

Additionally, if this is something other than a homework assignment, make sure you understand that this sort of trigger does not work correctly in a multi-user environment.




回答2:


just a wild guess

edit as Justin pointed out (thanks Justin) equality check

create or replace trigger "PASSENGER_BOOKING_T1"
AFTER
insert on "PASSENGER_BOOKING"
for each row
declare
   v_flight_id  FLIGHT.FLIGHT_ID%TYPE;
begin
IF (:NEW.CLASS_TYPE = 'ECO') THEN

SELECT F.ID into v_flight_id
FROM BOOKING B, JOURNEY_FLIGHT J, FLIGHT F 
WHERE B.ID = :NEW.BOOKING_ID -- note that I've made this up
AND B.JOURNEY_ID = J.JOURNEY_ID AND F.FLIGHT_ID = J.FLIGHT_ID;

UPDATE FLIGHT 
SET AVL_SEATS_ECOCLASS = (SEAT-1)
WHERE ID = v_flight_id;

END IF;
end;​


来源:https://stackoverflow.com/questions/8107854/oracle-triggers-update-at-another-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!