PL/PGSQL recoder une variable
Bonjour,
Tout d'abord je tiens à m'excuser si je ne poste pas au bon endroit (je n'ai pas vu de forum pour pl/pgsql).
Ma question maintenant :
J'essaie de recoder une variable age (de type integer) en classes (dans une nouvelle variable de type varchar).
Voici la fonction que j'ai créée :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| create or replace function ageclassesfunction() returns void as
$$
declare
agevalue integer;
begin
--ALTER TABLE personsample ADD COLUMN recodedage varchar(10);
FOR agevalue IN SELECT age FROM personsample
LOOP
IF agevalue between 0 and 9 THEN
update personsample set recodedage='[-10]';
ELSEIF agevalue between 10 and 19 THEN
update personsample set recodedage='[10-19]';
ELSEIF agevalue between 20 and 29 THEN
update personsample set recodedage='[20-29]';
ELSEIF agevalue between 30 and 39 THEN
update personsample set recodedage='[30-39]';
ELSEIF agevalue between 40 and 49 THEN
update personsample set recodedage='[40-49]';
ELSEIF agevalue between 50 and 59 THEN
update personsample set recodedage='[50-59]';
ELSEIF agevalue between 60 and 69 THEN
update personsample set recodedage='[60-69]';
ELSEIF agevalue between 70 and 79 THEN
update personsample set recodedage='[70-79]';
ELSEIF agevalue between 80 and 89 THEN
update personsample set recodedage='[80-89]';
ELSE
update personsample set recodedage='[90+]';
END IF;
END LOOP;
end
$$ LANGUAGE 'plpgsql'; |
Cette fonction s'exécute sans problème. Mais quand je l'appelle et que j'affiche la table, la variable recodedage prend la même valeur pour toutes les lignes. Et à chaque fois que je réexécute la fonction, la variable prend une autre valeur (toujours unique). Bien sûr l'âge est différent à chaque ligne et devrait implquer différentes classes.
Je ne comprends pas pourquoi. Est-ce un problème dans mes conditions ?
Merci d'avance,
Romain