简体   繁体   中英

Oracle - Error at line 4: PL/SQL: SQL Statement

I am trying to build a trigger for my database and I am getting an error and I suspect it is because of my into clause but I am not getting the reason. I was reading the documentation of it ( https://docs.oracle.com/cd/B19306_01/appdev.102/b14261/selectinto_statement.htm ) and there is saying:

By default, a SELECT INTO statement must return only one row. Otherwise, PL/SQL raises the predefined exception TOO_MANY_ROWS and the values of the variables in the INTO clause are undefined. Make sure your WHERE clause is specific enough to only match one row

Well, at least I am trying to be sure that my where clause is returning one and just one row. Can you give me some advice about it?

CREATE OR REPLACE TRIGGER t_ticket
INSTEAD OF INSERT ON V_buyTicket FOR EACH ROW
    declare ticketID number; busy number; seatRoom number;
 BEGIN

     select count(a.id_ticket) into busy , s.freeSeats into seatRoom 
    from assigned a
    inner join show e on (a.id_movie= e.id_movie)
    inner join rooms s on (e.id_room = s.id_room)
    where a.id_session = 1 AND a.id_movie = 1
    group by s.freeSeats;

    if(busy < seatRoom ) then
        ticketID := seq_ticket.NEXTVAL;
        insert into tickets(id_ticket, type, number, cc, store) values(ticketID, :new.type, :new.number, :new.cc, :new.store);
        insert into assigned (id_ticket, id_movie, id_session) values(ticketID, :new.id_movie, :new.id_session);
    else
        DBMS_OUTPUT.PUT_LINE('No available seats');
    end if;
 END;

Don't use group by if you want exactly one row returned:

select count(a.id_ticket), sum(s.freeSeats)
into busy, seatRoom 
from assigned a inner join
     show e
     on a.id_movie = e.id_movie inner join
     rooms s
     on e.id_room = s.id_room
where a.id_session = 1 and a.id_movie = 1;

An aggregation query without a group by always returns exactly one row. The alternative would be to include an explicit rownum = 1 in the WHERE clause.

My guess is that you conditions are not choosing exactly one room. You might want to check on the logic.

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