简体   繁体   English

Oracle-第4行错误:PL / SQL:SQL语句

[英]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. 我正在尝试为数据库建立触发器,但出现错误,我怀疑这是由于我的in子句引起的,但我没有得到原因。 I was reading the documentation of it ( https://docs.oracle.com/cd/B19306_01/appdev.102/b14261/selectinto_statement.htm ) and there is saying: 我正在阅读它的文档( https://docs.oracle.com/cd/B19306_01/appdev.102/b14261/selectinto_statement.htm ),并且说:

By default, a SELECT INTO statement must return only one row. 默认情况下,SELECT INTO语句必须仅返回一行。 Otherwise, PL/SQL raises the predefined exception TOO_MANY_ROWS and the values of the variables in the INTO clause are undefined. 否则,PL / SQL会引发预定义的异常TOO_MANY_ROWS,并且INTO子句中的变量值未定义。 Make sure your WHERE clause is specific enough to only match one row 确保您的WHERE子句足够具体,只能匹配一行

Well, at least I am trying to be sure that my where clause is returning one and just one row. 好吧,至少我要确保我的where子句返回一行且只有一行。 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: 如果只想返回一行,请不要使用group by

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. 没有group by的聚合查询总是返回恰好一行。 The alternative would be to include an explicit rownum = 1 in the WHERE clause. 另一种方法是在WHERE子句中包含一个显式的rownum = 1

My guess is that you conditions are not choosing exactly one room. 我的猜测是您的条件并非完全选择一个房间。 You might want to check on the logic. 您可能需要检查逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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