Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. CREATE FUNCTION fn_generar_cod_microbiologico() RETURNS character varying
  2. LANGUAGE plpgsql
  3. AS $$
  4. declare
  5. codigo varchar (13);
  6. maximo varchar(5);
  7. anio2 varchar (4);
  8. anio integer;
  9. begin
  10.  
  11. anio2 = extract(year from current_date);
  12. anio = to_number (anio2, '9999');
  13.  
  14. select LPAD((max(h.correlativo)+1)::text,5,'0') as max_corr into maximo from
  15. (select to_number(substring(codigo_analisis_microbiologico,4,5),'99999') correlativo from blh_analisis_microbiologico
  16. where
  17. to_number(substring(codigo_analisis_microbiologico,10,4),'9999') = anio) as h;
  18.  
  19. if maximo is null
  20. then
  21. maximo = '00001';
  22. end if;
  23.  
  24. codigo = 'AM-'|| maximo || '-' || anio;
  25. return codigo;
  26. end;
  27.  
  28. CREATE FUNCTION fn_generar_cod_receptor(id_blh integer, anio integer) RETURNS character varying
  29. LANGUAGE plpgsql
  30. AS $$
  31. declare
  32. codigo varchar (14);
  33. maximo varchar(5);
  34. siguiente integer;
  35. siguiente2 integer;
  36. id_blh2 varchar(3);
  37. begin
  38. id_blh2 = LPAD(id_blh::text,2,'0')||'%';
  39. select LPAD((max(h.correlativo)+1)::text,5,'0') as max_corr into maximo from (select to_number(substring(codigo_receptor,5,6),'99999') correlativo from blh_receptor
  40. where
  41. codigo_receptor like id_blh2
  42. and to_number(substring(codigo_receptor,11,4),'99999') = anio) as h;
  43.  
  44. if maximo is null
  45. then
  46. maximo = '00001';
  47. end if;
  48.  
  49. codigo = (select LPAD(id_blh::text,2,'0'))|| '-' || 'R' || maximo || '-' || anio;
  50. return codigo;
  51. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement