Précédent   Forum des professionnels en informatique > Bases de données > PostgreSQL
PostgreSQL Forum PostgreSQL. Avant de poster -> F.A.Q PostGreSQL Tutoriels PostGreSQL
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
'
Vieux 31/03/2005, 16h19   #1
Invité régulier
 
Inscription : janvier 2003
Messages : 36
Détails du profil
Informations forums :
Inscription : janvier 2003
Messages : 36
Points : 6
Points : 6
Par défaut condition if

Bonjour,

j'essaie d'utiliser des conditions dans une procédure stocké mais ça ne marche pas :
CREATE OR REPLACE FUNCTION list(varchar,varchar) RETURNS setof org AS
$body$
IF $2 <> 'ADM'
select * from organigramme where entorg like (select entorg from agent where matricule like $1) || '%';
else
select * from organigramme;
end
$body$
LANGUAGE 'sql' VOLATILE CALLED ON NULL INPUT SECURITY INVOKER;

quelqu'un aurait-il une idée ?

merci
pseudomh est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 31/03/2005, 16h26   #2
Membre éclairé
 
Avatar de piff62
 
Inscription : décembre 2003
Messages : 431
Détails du profil
Informations personnelles :
Âge : 26

Informations forums :
Inscription : décembre 2003
Messages : 431
Points : 362
Points : 362
La syntaxe a utiliser
Code :
1
2
3
4
5
6
 
IF condition THEN
    ...
ELSE
    ...
END IF;
piff62 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 31/03/2005, 16h37   #3
Invité régulier
 
Inscription : janvier 2003
Messages : 36
Détails du profil
Informations forums :
Inscription : janvier 2003
Messages : 36
Points : 6
Points : 6
j'avais déjà essayé mais ça ne marche pas.

CREATE OR REPLACE FUNCTION list(varchar,varchar) RETURNS setof org AS
$body$
IF $2 <> 'ADM' then
select * from org where entorg like (select entorg from agent where matricule like $1) || '%';
else
select * from org;
end if;
$body$
LANGUAGE 'sql' VOLATILE CALLED ON NULL INPUT SECURITY INVOKER;

=> il me met error at or near if
pseudomh est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 31/03/2005, 16h45   #4
Membre émérite
 
Avatar de hpalpha
 
Inscription : mars 2002
Messages : 770
Détails du profil
Informations forums :
Inscription : mars 2002
Messages : 770
Points : 833
Points : 833
il manque BEGIN .... END

Code :
1
2
3
4
5
6
7
8
9
10
11
CREATE OR REPLACE FUNCTION list(varchar,varchar) RETURNS setof org AS
$body$
BEGIN
IF $2 <> 'ADM' then
SELECT * FROM org WHERE entorg LIKE (SELECT entorg FROM agent WHERE matricule LIKE $1) || '%';
else
SELECT * FROM org;
end IF;
END
$body$
LANGUAGE 'sql' VOLATILE CALLED ON NULL INPUT SECURITY INVOKER;
hpalpha est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 31/03/2005, 16h51   #5
Invité régulier
 
Inscription : janvier 2003
Messages : 36
Détails du profil
Informations forums :
Inscription : janvier 2003
Messages : 36
Points : 6
Points : 6
...ça ne change rien, même message
pseudomh est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 31/03/2005, 17h01   #6
Membre émérite
 
Avatar de hpalpha
 
Inscription : mars 2002
Messages : 770
Détails du profil
Informations forums :
Inscription : mars 2002
Messages : 770
Points : 833
Points : 833
j'avais pas remarqué , mais tu prend comme langage 'sql' , je ne pense pas que begin, if, else, .... existent, c'est le plpgsql qui le permet
hpalpha est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 31/03/2005, 17h01   #7
Membre habitué
 
Inscription : mai 2002
Messages : 131
Détails du profil
Informations forums :
Inscription : mai 2002
Messages : 131
Points : 142
Points : 142
Forcément, un bloc "IF THEN ELSE" dans une procédure codée en sql, il va avoir du mal à comprendre

EDIT : arf, trop lent
Quentin est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 31/03/2005, 17h10   #8
Invité régulier
 
Inscription : janvier 2003
Messages : 36
Détails du profil
Informations forums :
Inscription : janvier 2003
Messages : 36
Points : 6
Points : 6
oui mais là il me met "SELECT query has no destination for result data"
pseudomh est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 31/03/2005, 17h33   #9
Membre habitué
 
Inscription : mai 2002
Messages : 131
Détails du profil
Informations forums :
Inscription : mai 2002
Messages : 131
Points : 142
Points : 142
Effectivement, c'est normal... Si on reste en plpgsql, il faut boucler sur ton select avec un for et faire une série de 'return next'...

Mais bon, il doit y avoir moyen de rester avec du SQL pur. Essaie comme ceci :

Code :
1
2
3
4
5
6
7
8
9
10
 
CREATE OR REPLACE FUNCTION list(varchar,varchar) RETURNS setof org AS
$body$
 
SELECT * FROM org
WHERE (case when $2 <> 'ADM' 
      then (entorg LIKE (SELECT entorg FROM agent WHERE matricule LIKE $1) || '%')
      else true end);
$body$
LANGUAGE 'sql' VOLATILE CALLED ON NULL INPUT SECURITY INVOKER;
Quentin est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 01/04/2005, 11h11   #10
Invité régulier
 
Inscription : janvier 2003
Messages : 36
Détails du profil
Informations forums :
Inscription : janvier 2003
Messages : 36
Points : 6
Points : 6
ça marche impec !!!

merci merci merci !!
pseudomh est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 05h48.


 
 
 
 
Partenaires

Hébergement Web