32 | | source:branches/STACOMIR0.3/requètes/operation_script_test_chevauchement_PLSQL.sql |
| 32 | |
| 33 | {{{#!sql |
| 34 | DROP TABLE IF EXISTS temp_operation; |
| 35 | CREATE TEMP TABLE temp_operation ( |
| 36 | ope_identifiant serial NOT NULL, |
| 37 | ope_dic_identifiant integer NOT NULL, |
| 38 | ope_date_debut timestamp(0) without time zone NOT NULL, |
| 39 | ope_date_fin timestamp(0) without time zone NOT NULL, |
| 40 | ope_organisme character varying(35), |
| 41 | ope_operateur character varying(35), |
| 42 | ope_commentaires text, |
| 43 | CONSTRAINT c_pk_ope PRIMARY KEY (ope_identifiant)); |
| 44 | |
| 45 | SELECT * FROM temp_operation where (ope_date_debut,ope_date_fin) OVERLAPS (ope_date_debut,ope_date_fin); |
| 46 | |
| 47 | SET CLIENT_ENCODING TO 'WIN1252'; |
| 48 | COPY temp_operation FROM 'C:/base/t_operation_ope_MONT2.csv' USING DELIMITERS ';' WITH CSV HEADER NULL AS ''; |
| 49 | |
| 50 | |
| 51 | --DROP FUNCTION chercheoverlaps() |
| 52 | CREATE OR REPLACE FUNCTION chercheoverlaps() RETURNS SETOF text AS |
| 53 | $BODY$ |
| 54 | DECLARE |
| 55 | nbChevauchements INTEGER ; |
| 56 | idope INTEGER ; |
| 57 | r temp_operation%rowtype; |
| 58 | resultat text; |
| 59 | BEGIN |
| 60 | FOR r IN SELECT * FROM temp_operation |
| 61 | LOOP |
| 62 | SELECT COUNT(*) INTO nbChevauchements |
| 63 | FROM temp_operation |
| 64 | WHERE (ope_date_debut, ope_date_fin) OVERLAPS (r.ope_date_debut, r.ope_date_fin); |
| 65 | IF (nbChevauchements > 1) THEN |
| 66 | resultat=r.ope_identifiant; |
| 67 | -- je vais chercher la deuxième opération incriminée |
| 68 | SELECT ope_identifiant INTO idope |
| 69 | FROM temp_operation |
| 70 | WHERE (ope_date_debut, ope_date_fin) OVERLAPS (r.ope_date_debut, r.ope_date_fin) |
| 71 | AND ope_identifiant<>r.ope_identifiant; |
| 72 | -- Il existe un chevauchement |
| 73 | RAISE NOTICE 'Les dates se chevauchent : premiere operation --->%',resultat; |
| 74 | RAISE NOTICE 'deuxième opération --->%',idope; |
| 75 | END IF ; |
| 76 | RETURN NEXT r; |
| 77 | END LOOP; |
| 78 | RETURN; |
| 79 | END; |
| 80 | $BODY$ |
| 81 | LANGUAGE 'plpgsql' ; |
| 82 | |
| 83 | |
| 84 | SELECT COUNT(*) FROM chercheoverlaps(); |
| 85 | }}} |