X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 06073D7275 for ; Mon, 29 Aug 2005 16:58:50 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 79345-06 for ; Mon, 29 Aug 2005 19:58:45 +0000 (GMT) Received: from window.monsterlabs.com (window.monsterlabs.com [216.183.105.176]) by svr1.postgresql.org (Postfix) with SMTP id 7B795D706A for ; Mon, 29 Aug 2005 16:58:41 -0300 (ADT) Received: (qmail 3260 invoked from network); 29 Aug 2005 19:58:41 -0000 Received: from pcp0012204803pcs.blairblvd.tn.nash.comcast.net (HELO ?192.168.15.110?) (69.245.49.69) by 0 with SMTP; 29 Aug 2005 19:58:41 -0000 In-Reply-To: References: Mime-Version: 1.0 (Apple Message framework v734) X-Priority: 3 Content-Type: text/plain; charset=WINDOWS-1252; delsp=yes; format=flowed Message-Id: Cc: Content-Transfer-Encoding: quoted-printable From: "Thomas F. O'Connell" Subject: Re: Numerical variables in pqsql statements Date: Mon, 29 Aug 2005 14:58:38 -0500 To: Michael Schmidt X-Mailer: Apple Mail (2.734) X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.02 required=5 tests=[AWL=0.020] X-Spam-Level: X-Archive-Number: 200508/269 X-Sequence-Number: 22497 Michael, PL/pgSQL variable interpolation works similarly to that in other =20 popular programming languages. If you have a statement -- whether =20 it's PERFORM, SELECT INTO, or EXECUTE -- a variable will get =20 interpolated during parsing if not escaped in a string. Per the =20 documentation, dynamic values in dynamic queries require special =20 handling since they might themselves contain quotes: http://www.postgresql.org/docs/8.0/static/plpgsql-=20 statements.html#PLPGSQL-STATEMENTS-EXECUTING-DYN But your temp table statement below is fine if you know that there =20 will never be quotes in refer_num. And it would work similarly if you built it dynamically via string =20 concatenation as a query to be executed with quote_literal escaping =20 for refer_num. Typically, you need to protect yourself against user input to a =20 function. If you're computing values in the function body that you =20 know to be safe or passing in safe values generated elsewhere in the =20 application, you're less likely to need to quote your variables =20 explicitly or to build queries dynamically. -- Thomas F. O'Connell Co-Founder, Information Architect Sitening, LLC Strategic Open Source: Open Your i=99 http://www.sitening.com/ 110 30th Avenue North, Suite 6 Nashville, TN 37203-6320 615-469-5150 615-469-5151 (fax) On Aug 21, 2005, at 7:43 PM, Michael Schmidt wrote: > Folks, > I'm sure this is dumb, but I'm a little confused about use of =20 > numerical variables in pqsql. It was my impression one had to use =20 > EXECUTE on a concatenated string including quote_literal() for the =20 > variable containing the value. This can be quite a bit of =20 > trouble. I just wrote a function that included the statement : > > CREATE TEMPORARY TABLE author_names AS > SELECT ra.ref_auth_key, a.last_name, a.first_name > FROM ref_auth ra INNER JOIN authors a > ON (ra.author_num =3D a.author_key) > WHERE ra.ref_num =3D refer_num; > > where refer_num is integer. This worked (surprisingly, to me). =20 > So, what is the rule regarding variables? Would this query work if =20= > I concatenated a string together, including quote_literal=20 > (refer_num) and then EXECUTEd it? > > Thanks and sorry to be so stupid.