I'm completely new to psql. And when I use http://sqlfiddle.com/ to do some homework, the system return this error.
ERROR: syntax error at or near "RETURNS"
Any help is appreciated. Here is my psql:
CREATE TABLE HotelStays
(roomNum INTEGER NOT NULL,
arrDate DATE NOT NULL,
depDate DATE NOT NULL,
guestName CHAR(30) NOT NULL,
PRIMARY KEY (roomNum, arrDate))
;
CREATE OR REPLACE FUNCTION stopInsert RETURNS trigger AS
$body$
DECLARE
availableArrDate DATE;
checkRoomNum INTEGER;
BEGIN
if (NEW.arrDate >= NEW.depDate) then
return null;
end if;
checkRoomNum = NEW.roomNum;
select h.depDate into availableArrDate
from HotelStays h
where h.roomNum = checkRoomNum
order by h.depDate DESC
LIMIT 1;
if (availableArrDate > NEW.arrDate)
return null;
end if;
END;
$body$ LANGUAGE plpgsql;
create trigger stopInsert before insert ON HotelStays
For each row
execute procedure stopInsert();
