Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id CF9A7632199 for ; Fri, 17 Apr 2009 06:26:41 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 33226-03 for ; Fri, 17 Apr 2009 06:26:31 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from wf-out-1314.google.com (wf-out-1314.google.com [209.85.200.172]) by mail.postgresql.org (Postfix) with ESMTP id B0F2E62DBDD for ; Fri, 17 Apr 2009 06:26:35 -0300 (ADT) Received: by wf-out-1314.google.com with SMTP id 29so739232wff.28 for ; Fri, 17 Apr 2009 02:26:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:message-id:subject:from:to:content-type :content-transfer-encoding; bh=po7BmYSyqzE8ez5vmTnaR3Ns/UDi6OhpaDjQfKrGynI=; b=xkOMngeo9hys2moUuiYepYZ7R/LzeQNrpECJTGMxlrNH3KLZHEPy/P0ecGcmtgpGms IULxQGp8GKtzFN/xQ81OeMOfe2ioUnkHL1hfVfa2fJtdsToTB2MK8hqIQC99U1tt9GVR aGPNRdz/E3oKhQyOeobywXe1c+w1uUVX07pmw= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; b=Z7RKhQIHJK6JeswWRhOExmsbIpJxjpNnSxj3GNKAKLsBuFqZFF2EfQEJrq0KQR8ugW KYBhCkNPAf1+OER2o2iB8aEfyTXxQ7FHu6InWXRB+ZOnQDi84I3S8Dl4t7BX1pTjwQzz bSk9abp+sSmiRpafr1UnYzFpjlcfYXexQw9Ks= MIME-Version: 1.0 Received: by 10.142.76.4 with SMTP id y4mr3256950wfa.172.1239960393991; Fri, 17 Apr 2009 02:26:33 -0700 (PDT) In-Reply-To: <27f31620904162252t650c5205g1305a3cdbce15c1@mail.gmail.com> References: <27f31620904160102j7668c947ta435601dd8751c6e@mail.gmail.com> <16591.1239890835@sss.pgh.pa.us> <27f31620904162252t650c5205g1305a3cdbce15c1@mail.gmail.com> Date: Fri, 17 Apr 2009 18:26:33 +0900 Message-ID: <27f31620904170226u6a953fe1g372eb9bbd26f967a@mail.gmail.com> Subject: Re: Bound parameter is not substituted From: ning To: pgsql-odbc@postgresql.org Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests=none X-Spam-Level: X-Archive-Number: 200904/19 X-Sequence-Number: 9838 Hello all, I finally found a way to create function dynamically from client side through ODBC connection by referring to http://www.postgresql.org/docs/8.3/interactive/plpgsql-porting.html example 38-6: definiton of function cs_update_referrer_type_proc() The psql command line log is pasted below for reference. First, a stored function function_factory() needs to be installed on server side, which is used to created function dynamically. Please refer to log below for definition of funciton_factory(). Second, we call function_factory() from client side to create a new functio= n whose name, "declare" part and "begin-end" part are dynamically constructed= . --------- SQLPrepare( hstmt, "select function_factory('function_eval', 'pid integer;', 'pid :=3D ' || ? || ';');", SQL_NTS ); --------- 'function_eval': function name. 'pid integer': "declare" part of function_eval(). 'pid :=3D ' || ? || ';': "begin-end" part of function_eval(), "?" can be substituted. Third, execute function_eval() -------- SQLPrepare( hstmt, "select function_eval();", SQL_NTS ); -------- I know it's hard to use and maybe not the best solution for dynamic functio= n. Any idea for simpler solution will be appreciated. In fact, if inline SQL PL were supported as DB2: http://publib.boulder.ibm.com/infocenter/db2luw/v9/index.jsp?topic=3D/com.i= bm.db2.udb.apdv.sql.doc/doc/r0020490.htm The solution would be straightforward. Someone talked about Inline PL/pgSQL in 2005: http://www.mail-archive.com/pgsql-hackers@postgresql.org/msg58796.html But I cannot find anything about implementation in this field. Best regards. ning -------log------------ sample=3D# CREATE OR REPLACE FUNCTION function_factory(text, text, text) RETURNS void AS $func$ sample$# DECLARE sample$# func_body text; sample$# func_cmd text; sample$# BEGIN sample$# func_body :=3D 'declare '; sample$# func_body :=3D func_body || $2; sample$# sample$# func_body :=3D func_body || 'BEGIN '; sample$# func_body :=3D func_body || $3; sample$# func_body :=3D func_body || ' END;'; sample$# sample$# func_cmd :=3D sample$# 'CREATE OR REPLACE FUNCTION ' || $1 || sample$# '() RETURNS void AS ' sample$# || quote_literal(func_body) sample$# || ' LANGUAGE plpgsql;' ; sample$# sample$# EXECUTE func_cmd; sample$# END; sample$# $func$ LANGUAGE plpgsql; CREATE FUNCTION sample=3D# select function_factory('function_eval', 'pid integer;', 'pid :=3D ' || 10 || ';'); function_factory ------------------ (1 row) sample=3D# select function_eval(); function_eval --------------- (1 row) sample=3D# ------------------ On Fri, Apr 17, 2009 at 2:52 PM, ning wrote: > Thank you very much for your reply. > > Now I understand the cause of the error, but I seems funciton body > cannot be constructed dynamically. > I tried: > ----------- > ss << > =C2=A0 "create or replace function plpgsql_setDocAttrs() returns void as = ' " > =C2=A0 "declare " > =C2=A0 "pid integer; " > =C2=A0 "begin " > =C2=A0 "pid :=3D ' " > =C2=A0 " || ? || " > =C2=A0 "'; " > =C2=A0 "end;" > =C2=A0 "' language plpgsql volatile;" > ---------- > > When I bind a variable, with value 10, to ?, the string after substitutio= n is > ---------- > create or replace function plpgsql_setDocAttrs() returns void as > 'declare pid integer;begin pid :=3D ' =C2=A0|| 10 || '; end;' language > plpgsql volatile; > ---------- > I got the following error: > ERROR: =C2=A0syntax error at or near "||" > > String concatenation operator "||" is not allowed in "create function" > statement, which breaks syntax. > Is there any way to force string concatenation before function definition= ? > So that the =C2=A0function body is firstly concatenated to > --------- > 'declare pid integer;begin pid :=3D 10; end;' > -------- > Then "create function" statement is executed as > ------- > create or replace function plpgsql_setDocAttrs() returns void as ' > declare pid integer;begin pid :=3D 10; end;' language plpgsql volatile; > ------ > > Any suggestion or idea is appreciated. > > Best regards. > > ning > > On Thu, Apr 16, 2009 at 11:07 PM, Tom Lane wrote: >> ning writes: >>> I am trying to create a stored function through ODBC connection, >>> But the parameter marker inside the function is not substituted. >> >> You've written a question mark inside a string literal. =C2=A0It would b= e >> a bug for the software to consider that to be a parameter marker. >> It's just a question mark. >> >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0regards, tom lane >> >