Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CREATE OR REPLACE FUNCTION vallee.newemprunt(integer, integer, integer)
- RETURNS integer
- LANGUAGE plpgsql
- AS $function$
- declare
- res integer;
- nbExemplaire integer;
- nbLecteur integer;
- nbEmprunteur integer;
- nbLecteurDejaEmprunte integer;
- begin
- res = 0;
- select into nbExemplaire count(numero) from exemplaire where idlivre = $3;
- if nbExemplaire = 0 then
- res = 1;
- select into nbLecteur count(idelecteur) from lecteur where idelecteur = $2;
- if nbLecteur = 0 then
- res = 2;
- end if;
- end if;
- select into nbEmprunteur count(numero) from exemplaire where numero = $1 and idlivre = $3 and dateretour is null;
- if nbEmprunteur = 0 then
- res = 3;
- select into nbEmprunteur count(numero) from exemplaire where emprunteur = $2;
- if nbEmprunteur >= 1 then
- res = 4;
- end if;
- end if;
- if res = 0 then
- update exemplaire set dateretour = date(now())+15, emprunteur = $2
- where numero = $1
- and idlivre = $3;
- end if;
- return res;
- end;
- $function$
- ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement