Advertisement
Guest User

xaviretra

a guest
Oct 24th, 2014
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.61 KB | None | 0 0
  1. create table cotxes(
  2.     matricula char(10) primary key,
  3.     marca char(20) not null,
  4.     model char(20) not null,
  5.     categoria integer not null,
  6.     color char(10),
  7.     any_fab integer
  8.     );
  9. create table treballadors(
  10.     dni char(8) primary key,
  11.     nom char(30) not null,
  12.     sou_base real not null,
  13.     plus real not null
  14.     );
  15. create table lloguers_actius(
  16.     matricula char(10) primary key references cotxes,
  17.     dni char(8) not null references treballadors,
  18.     num_dies integer not null,
  19.     preu_total real not null
  20.     );
  21. create table missatgesExcepcions(
  22.     num integer,
  23.     texte varchar(50)
  24.     );
  25. insert into missatgesExcepcions values(1,'No hi ha cap tupla dins del interval demanat');
  26. insert into missatgesExcepcions values(2, 'Error intern');
  27.  
  28. insert into cotxes values ('1111111111','Audi','A4',1,'Vermell',1998);
  29. insert into cotxes values ('2222222222','Audi','A3',2,'Blanc',1998);
  30. insert into cotxes values ('3333333333','Volskwagen','Golf',2,'Blau',1990);
  31. insert into cotxes values ('4444444444','Toyota','Corola',3,'groc',1999);
  32. insert into cotxes values ('5555555555','Honda','Civic',3,'Vermell',2000);
  33. insert into cotxes values ('6666666666','BMW','Mini',2,'Vermell',2000);
  34.  
  35. insert into treballadors values ('22222222','Joan',1700,150);
  36.  
  37. insert into lloguers_actius values ('1111111111','22222222',7,750);
  38. insert into lloguers_actius values ('2222222222','22222222',5,550);
  39. insert into lloguers_actius values ('3333333333','22222222',4,450);
  40. insert into lloguers_actius values ('4444444444','22222222',8,850);
  41. insert into lloguers_actius values ('5555555555','22222222',2,250);
  42.  
  43.  
  44. CREATE TYPE FILA_TREB AS (
  45.     dni_treb VARCHAR(8),
  46.     nom_treb VARCHAR(30),
  47.     sou_treb real,
  48.     plus_sou_treb real,
  49.     matricula_treb VARCHAR(10));
  50.    
  51. CREATE OR REPLACE FUNCTION llistat_treb(dni_i varchar(8), dni_s varchar(8)) RETURN SETOF
  52. RETURNS setof FILA_TREB AS $$
  53. DECLARE treb FILA_TREB;
  54.  
  55. BEGIN
  56.  
  57. FOR treb IN SELECT *  
  58.       FROM treballadors t
  59.       WHERE t.dni >= dni_i  
  60.       AND t.dni<= dni_s
  61.       ORDER BY t.dni
  62. LOOP   
  63.        IF((SELECT COUNT (*)
  64.        FROM lloguers l
  65.        WHERE t.DNI = l.dni) >= 5)
  66.        THEN
  67.        FOR treb.matricula IN SELECT L1.matricula
  68.             from lloguers L1
  69.             where t.dni = L1.dni
  70.             order by L1.matricula
  71.      LOOP
  72.       RETURN NEXT treb
  73.      END LOOP
  74.         ELSE THEN
  75.  
  76.  
  77.  
  78. RETURN NEXT tr;
  79. END LOOP;
  80. IF (quants < 1) THEN
  81. SELECT texte INTO missatge FROM missatgesExcepcions WHERE num=1;
  82. RAISE EXCEPTION '%',missatge;
  83. END IF;
  84. RETURN;
  85.  
  86. EXCEPTION
  87. WHEN RAISE_EXCEPTION THEN
  88. RAISE EXCEPTION '%',SQLERRM;
  89. WHEN OTHERS THEN
  90. SELECT texte INTO missatge FROM missatgesExcepcions WHERE num=2;
  91. RAISE EXCEPTION '%',missatge;
  92. END;
  93. $$LANGUAGE plpgsql;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement