0
I am migrating from Mysql to Postgresql and trying to create the following Trigger:
CREATE OR REPLACE FUNCTION verificaReservaQuarto()
RETURNS trigger
AS $$
begin
declare ocupado int;
declare consultaReserva int;
select count(*) into @consultaReserva from reserva_quarto R where R.dataSaida is null and R.dataEntrada is not null and new.idConsulta = R.idConsulta;
select count(*) into @ocupado from reserva_quarto R where R.dataSaida is null and R.dataEntrada is not null and new.idQuarto = R.idQuarto;
set new.dataSaida = null;
if (new.dataEntrada < now() or (@ocupado > 0) or (@consultaReserva > 0)) then
????????????????????????????
else
return NEW;
end if;
end;
$$ LANGUAGE plpgsql;
However, the following error is occurring:
ERROR: syntax error at or near "*"
LINE 7: select count(*) into @consultaReserva from reserva_quarto R ...
^
CONTEXT: invalid type name "count(*) into @consultaReserva from reserva_quarto R where R.dataSaida is null and R.dataEntrada is "
SQL state: 42601
Character: 149
Where I put "????????????????" I would not let the insertion take place, because in my business rule the room would be occupied.
Follow the tables used on Rigger:
CREATE TABLE reserva_quarto (
dataEntrada timestamp not null,
dataSaida timestamp,
idConsulta INTEGER not null,
idQuarto INTEGER not null,
FOREIGN KEY(idQuarto) REFERENCES quartos (idQuarto),
FOREIGN KEY(idConsulta) REFERENCES consulta (idConsulta)
);
CREATE TABLE consulta (
idConsulta SERIAL PRIMARY KEY,
prescricao VARCHAR(1000) not null,
dataConsulta timestamp not null ,
exame int not null,
idFuncGeral INTEGER not null,
idPaciente INTEGER not null,
idMedico INTEGER not null,
FOREIGN KEY(idMedico) REFERENCES medicos (idMedico)
);
CREATE TABLE quartos (
idQuarto SERIAL PRIMARY KEY,
frigobar INTEGER not null,
TV INTEGER not null,
idAla INTEGER not null
);
Assuming your Rigger is BEFORE then make one
return NULL;
.– anonimo