Je cherche à effectuer une procedure stockée écrite en Pl/PgSQL.
Cette procédure stockée devrait me permettre de compter les occurences de
caractères ([a-z][0-9]#%$) dans une chaine, afin de me simplifier certains
traitements futurs.
Malheureusement, lorsque j'essaye d'executer cette procédure, j'obtiens
l'erreur suivante :
ERROR: syntax error at or near "LOOP"
CONTEXT: compile of PL/pgSQL function "add_chars" near line 26
Il y aurait donc une erreur, qui pourrait se situer aux alentours de mon
END LOOP; . Hélas, j'ai beau chercher, je ne parviens pas à mettre la main
sur ma coquille...
Je poste donc ici en espérant que quelqu'un sera plus inspiré que moi...
CREATE TABLE db_part_chars
(
id_part integer NOT NULL,
id_fourn integer NOT NULL,
chars_nb VARCHAR(255)
);
CREATE FUNCTION add_chars(integer, integer, text) RETURNS void
AS '
DECLARE
new_id_part ALIAS FOR $1;
new_id_fourn ALIAS FOR $2;
ref_a_nu ALIAS FOR $3;
col INTEGER;
chaine text;
BEGIN
chaine := repeat(chr(0), 39);
INSERT INTO db_part_chars(id_part, id_fourn)
VALUES (new_id_part, new_id_fourn);
FOR pos IN 1..char_length(ref_a_nu) LOOP
col := ascii(substr(ref_a_nu, pos, 1));
IF (col >= 35 AND col <= 37) THEN
col := col + 2;
chaine := substr(chaine, 1, col - 1) ||
chr(ascii(substr(chaine, col, 1)) + 1) || substr(chaine, col + 1);
ELSE IF (col >= 48 AND col <= 57) THEN
col := col - 21;
chaine := substr(chaine, 1, col - 1) ||
chr(ascii(substr(chaine, col, 1)) + 1) || substr(chaine, col + 1);
ELSE IF (col >= 65 AND col <= 90) THEN
col := col - 64;
chaine := substr(chaine, 1, col - 1) ||
chr(ascii(substr(chaine, col, 1)) + 1) || substr(chaine, col + 1);
ELSE IF (col >= 97 AND col <= 122) THEN
col := col - 96;
chaine := substr(chaine, 1, col - 1) ||
chr(ascii(substr(chaine, col, 1)) + 1) || substr(chaine, col + 1);
END IF;
END LOOP;
UPDATE db_part_chars SET chars_nb=chaine WHERE id_part=new_id_part;
RETURN;
END;
'
LANGUAGE plpgsql;