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