小编Jos*_*ose的帖子

触发错误时回滚事务

我正在尝试检查将要插入系统的房间在该日期是否已经租用。我已经考虑过计算与房间号和日期匹配的行,然后回滚事务。但是我收到以下错误,即使我已经更改了代码以引发用户定义的异常:

ERROR:  cannot begin/end transactions in PL/pgSQL
HINT: Use a BEGIN block with an EXCEPTION clause instead.
CONTEXT: PL/pgSQL function "checkRoom"() line 17 at SQL statement
Run Code Online (Sandbox Code Playgroud)
CREATE OR REPLACE FUNCTION "checkRoom"() RETURNS TRIGGER AS
$BODY$
DECLARE 
    counter integer;
  BEGIN
    SELECT COUNT("num_sesion")
    FROM "Sesion"
    INTO counter
    WHERE "Room_Name"=NEW."Room_Name" AND "Date"=NEW."Date";

    IF (counter> 0) THEN -- Probably counter>1 as it's triggered after the transaction..
        raise notice 'THERE'S A ROOM ALREADY!!';
        raise exception 'The room is rented at that date';
    END IF;
    RETURN …
Run Code Online (Sandbox Code Playgroud)

sql postgresql triggers plpgsql unique-constraint

4
推荐指数
2
解决办法
9391
查看次数

标签 统计

plpgsql ×1

postgresql ×1

sql ×1

triggers ×1

unique-constraint ×1