2017-06-23 2 views
0

J'ai écrit la fonction suivante qui renvoie les enregistrements d'une autre table 'parameters_ltree_ {time_created}' où time_created est une colonne de la table historical_ltree.Suppression des guillemets de la chaîne concaténée dans l'identificateur plpgsql

CREATE OF REPLAE function get_my_path(date_string text, path_arg ltree) RETURNS SETOF ltree AS 
$BODY$ 
DECLARE 
    p text; 
    d text; 
BEGIN 
    d := quote_ident(date_string); 
    p := 'parameters_ltree_'; 
    RETURN QUERY EXECUTE format(
    'SELECT path from %I' || '%I 
    WHERE path = %I 
    ORDER BY path 
    LIMIT 1' 
    , p, date_string, path_arg); 

END 
$BODY$ 
LANGUAGE plpgsql; 

SELECT id, path, get_my_path(to_char(time_created, 'YYYYMMDD')), path) from historical_ltree 

La fonction compile, mais quand je lance la requête SELECT en bas, il jette une erreur de syntaxe:

ERROR: relation "parameters_ltree_" does not exist 
LINE 1: SELECT path FROM parameters_ltree_"20161201" 
         ^

J'ai essayé de nombreuses méthodes pour obtenir cette concaténation pour fonctionner correctement mais sans disponible, y compris en passant la chaîne de date comme un numérique.

Répondre

0

Essayez de changer:

RETURN QUERY EXECUTE format(
    'SELECT path from %I 
    WHERE path = %L 
    ORDER BY path 
    LIMIT 1' 
    , 'parameters_ltree_'||date_string, path_arg); 

https://www.postgresql.org/docs/current/static/functions-string.html

s formats the argument value as a simple string. A null value is treated as an empty string.

I treats the argument value as an SQL identifier, double-quoting it if necessary. It is an error for the value to be null (equivalent to quote_ident).

L quotes the argument value as an SQL literal. A null value is displayed as the string NULL, without quotes (equivalent to quote_nullable).

+0

merci brillant! – Haris

+0

sûr. pas de soucis –

0

Vous pouvez essayer cette

p := 'parameters_ltree_'||date_string; 
    RETURN QUERY EXECUTE format(
    'SELECT path from %I 
    WHERE path = %I 
    ORDER BY path 
    LIMIT 1' 
    , p, path_arg);