Skip to content

Instantly share code, notes, and snippets.

@guidani
Created July 10, 2021 19:35
Show Gist options
  • Save guidani/c3e0a6c6ca70310fd60c8ba781bc7102 to your computer and use it in GitHub Desktop.
Save guidani/c3e0a6c6ca70310fd60c8ba781bc7102 to your computer and use it in GitHub Desktop.
questao2
-- trigger function
CREATE OR REPLACE FUNCTION f_altera_status_reserva()
RETURNS TRIGGER AS $$
DECLARE
titulo_reservado int;
codigo_do_leitor int;
BEGIN
--
select cod_tit into titulo_reservado from livro where cod_livro in (select cod_livro from item_emprestimo);
select cod_leitor into codigo_do_leitor from emprestimo where cod_emprestimo in (select cod_emprestimo from item_emprestimo);
-- codigo
UPDATE RESERVA SET STATUS='I' WHERE cod_tit = titulo_reservado and cod_leitor = codigo_do_leitor;
RETURN NEW;
END;
$$ LANGUAGE 'plpgsql';
-- TRIGGER
CREATE TRIGGER t_altera_status_reserva
after INSERT or update
ON item_emprestimo
FOR EACH ROW EXECUTE PROCEDURE f_altera_status_reserva();
-- testes
insert into emprestimo(cod_emprestimo, cod_leitor, cod_func, dt_emprestimo, dt_prev_devolucao, quant_livro) values
(8, 2, 1, now(), '2021-07-10', 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment