IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

PostgreSQL Discussion :

fonction en plpgsql


Sujet :

PostgreSQL

  1. #1
    Membre du Club
    Profil pro
    Inscrit en
    Novembre 2002
    Messages
    64
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2002
    Messages : 64
    Points : 53
    Points
    53
    Par défaut fonction en plpgsql
    bonjour,
    j'ai créé une fonction en plpgsql (mon outil d'interfaçage postgres est pgadminIII). mon problème est que le texte de ma requete est créé selon les paramètres d'entrée renseignés et je n'arrive pas à concaténer mes chaines :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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 fctclassement(chpnumplu text, chpnumrayon text, chpca text, chpdatecumul text, chpmois text, chpannee text, nomtable text, numplurech int4, numrayrech int4, moisrech int4, anneerech int4, daterech text)
      RETURNS int4 AS
    $BODY$
    DECLARE
    	cpt record;
    	rangcour integer;
    	txtreq text;
    BEGIN
    	txtreq := 'SELECT ' ||chpnumplu|| ' as "numplu" FROM ' ||nomtable|| ' WHERE ' ||chpnumrayon|| ' = ' ||numrayrech||
    	IF chpdatecumul <> '' THEN
    		' and ' ||chpdatecumul|| ' = ' ||daterech||
    	ELSE
    		IF chpmois <> '' THEN
    			' and ' ||chpmois|| ' = ' ||moisrech||
    		END IF;
    		' and ' ||chpannee|| ' = ' ||anneerech||
    	END IF;
    	 ' ORDER BY ' ||chpca|| ' DESC;';
     
    	rangcour := 1;
    	FOR cpt IN EXECUTE txtreq LOOP
    		IF cpt.numplu = numplurech THEN
    			EXIT;
    		END IF;
    		rangcour := rangcour + 1;
    	END LOOP;
    	RETURN rangcour;
    END;
    $BODY$
      LANGUAGE 'plpgsql' VOLATILE;
    ALTER FUNCTION fctclassement(chpnumplu text, chpnumrayon text, chpca text, chpdatecumul text, chpmois text, chpannee text, nomtable text, numplurech int4, numrayrech int4, moisrech int4, anneerech int4, daterech text) OWNER TO postgres;
    Si quelqu'un s'y connait en plpgsql, je suis preneuse!!
    bonne journée

  2. #2
    Expert confirmé
    Homme Profil pro
    Inscrit en
    Septembre 2006
    Messages
    2 937
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations forums :
    Inscription : Septembre 2006
    Messages : 2 937
    Points : 4 358
    Points
    4 358
    Par défaut
    Citation Envoyé par 78alex78
    bonjour,
    j'ai créé une fonction en plpgsql (mon outil d'interfaçage postgres est pgadminIII). mon problème est que le texte de ma requete est créé selon les paramètres d'entrée renseignés et je n'arrive pas à concaténer mes chaines :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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 fctclassement(chpnumplu text, chpnumrayon text, chpca text, chpdatecumul text, chpmois text, chpannee text, nomtable text, numplurech int4, numrayrech int4, moisrech int4, anneerech int4, daterech text)
      RETURNS int4 AS
    $BODY$
    DECLARE
    	cpt record;
    	rangcour integer;
    	txtreq text;
    BEGIN
    	txtreq := 'SELECT ' ||chpnumplu|| ' as "numplu" FROM ' ||nomtable|| ' WHERE ' ||chpnumrayon|| ' = ' ||numrayrech||
    	IF chpdatecumul <> '' THEN
    		' and ' ||chpdatecumul|| ' = ' ||daterech||
    	ELSE
    		IF chpmois <> '' THEN
    			' and ' ||chpmois|| ' = ' ||moisrech||
    		END IF;
    		' and ' ||chpannee|| ' = ' ||anneerech||
    	END IF;
    	 ' ORDER BY ' ||chpca|| ' DESC;';
     
    	rangcour := 1;
    	FOR cpt IN EXECUTE txtreq LOOP
    		IF cpt.numplu = numplurech THEN
    			EXIT;
    		END IF;
    		rangcour := rangcour + 1;
    	END LOOP;
    	RETURN rangcour;
    END;
    $BODY$
      LANGUAGE 'plpgsql' VOLATILE;
    ALTER FUNCTION fctclassement(chpnumplu text, chpnumrayon text, chpca text, chpdatecumul text, chpmois text, chpannee text, nomtable text, numplurech int4, numrayrech int4, moisrech int4, anneerech int4, daterech text) OWNER TO postgres;
    Si quelqu'un s'y connait en plpgsql, je suis preneuse!!
    bonne journée
    utilisez quote_literal() et quote_ident()

    exemple :

    EXECUTE ''UPDATE tbl SET ''
    || quote_ident(fieldname)
    || '' = ''
    || quote_literal(newvalue)
    || '' WHERE ...'';

Discussions similaires

  1. Fonctions de plpgsql
    Par KonTiKI dans le forum PostgreSQL
    Réponses: 2
    Dernier message: 18/02/2009, 12h06
  2. [MySQL] Appel de fonction plpgsql ou plsql...
    Par Empty_body dans le forum PHP & Base de données
    Réponses: 3
    Dernier message: 02/01/2006, 18h56
  3. Fonction plpgsql
    Par Empty_body dans le forum PostgreSQL
    Réponses: 2
    Dernier message: 30/12/2005, 11h19
  4. [ debutant PLPGSQL ] fonction plpgsql
    Par diableblanc dans le forum PostgreSQL
    Réponses: 12
    Dernier message: 01/06/2005, 12h54
  5. [plpgsql] transaction dans les fonctions ?
    Par hpghost dans le forum PostgreSQL
    Réponses: 3
    Dernier message: 27/06/2004, 16h56

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo