pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Numerical variables in pqsql statements
3+ messages / 2 participants
[nested] [flat]

* Numerical variables in pqsql statements
@ 2005-08-22 00:43 Michael Schmidt <MichaelMSchmidt@msn.com>
  2005-08-29 19:58 ` Re: Numerical variables in pqsql statements Thomas F. O'Connell <tfo@sitening.com>
  0 siblings, 1 reply; 3+ messages in thread

From: Michael Schmidt @ 2005-08-22 00:43 UTC (permalink / raw)
  To: pgsql-sql

Folks,
I'm sure this is dumb, but I'm a little confused about use of numerical variables in pqsql.  It was my impression one had to use EXECUTE on a concatenated string including quote_literal() for the variable containing the value.  This can be quite a bit of 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 = a.author_key) 
      WHERE ra.ref_num = refer_num;

where refer_num is integer.  This worked (surprisingly, to me).  So, what is the rule regarding variables?  Would this query work if I concatenated a string together, including quote_literal(refer_num) and then EXECUTEd it?

Thanks and sorry to be so stupid.

^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Numerical variables in pqsql statements
  2005-08-22 00:43 Numerical variables in pqsql statements Michael Schmidt <MichaelMSchmidt@msn.com>
@ 2005-08-29 19:58 ` Thomas F. O'Connell <tfo@sitening.com>
  2005-08-30 15:13   ` Re: Numerical variables in pqsql statements Thomas F. O'Connell <tfo@sitening.com>
  0 siblings, 1 reply; 3+ messages in thread

From: Thomas F. O'Connell @ 2005-08-29 19:58 UTC (permalink / raw)
  To: Michael Schmidt <MichaelMSchmidt@msn.com>; +Cc: pgsql-sql

Michael,

PL/pgSQL variable interpolation works similarly to that in other  
popular programming languages. If you have a statement -- whether  
it's PERFORM, SELECT INTO, or EXECUTE -- a variable will get  
interpolated during parsing if not escaped in a string. Per the  
documentation, dynamic values in dynamic queries require special  
handling since they might themselves contain quotes:

http://www.postgresql.org/docs/8.0/static/plpgsql- 
statements.html#PLPGSQL-STATEMENTS-EXECUTING-DYN

But your temp table statement below is fine if you know that there  
will never be quotes in refer_num.

And it would work similarly if you built it dynamically via string  
concatenation as a query to be executed with quote_literal escaping  
for refer_num.

Typically, you need to protect yourself against user input to a  
function. If you're computing values in the function body that you  
know to be safe or passing in safe values generated elsewhere in the  
application, you're less likely to need to quote your variables  
explicitly or to build queries dynamically.

--
Thomas F. O'Connell
Co-Founder, Information Architect
Sitening, LLC

Strategic Open Source: Open Your i™

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  
> numerical variables in pqsql.  It was my impression one had to use  
> EXECUTE on a concatenated string including quote_literal() for the  
> variable containing the value.  This can be quite a bit of  
> 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 = a.author_key)
>       WHERE ra.ref_num = refer_num;
>
> where refer_num is integer.  This worked (surprisingly, to me).   
> So, what is the rule regarding variables?  Would this query work if  
> I concatenated a string together, including quote_literal 
> (refer_num) and then EXECUTEd it?
>
> Thanks and sorry to be so stupid.




^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Numerical variables in pqsql statements
  2005-08-22 00:43 Numerical variables in pqsql statements Michael Schmidt <MichaelMSchmidt@msn.com>
  2005-08-29 19:58 ` Re: Numerical variables in pqsql statements Thomas F. O'Connell <tfo@sitening.com>
@ 2005-08-30 15:13   ` Thomas F. O'Connell <tfo@sitening.com>
  0 siblings, 0 replies; 3+ messages in thread

From: Thomas F. O'Connell @ 2005-08-30 15:13 UTC (permalink / raw)
  To: Michael Schmidt <MichaelMSchmidt@msn.com>; +Cc: pgsql-sql

Well, browse through this list:

http://www.postgresql.org/docs/books/

I can't make any recommendations, as I am fairly familiar with the  
online documentation, which, when supported by the community, seems  
to be pretty good.

--
Thomas F. O'Connell
Co-Founder, Information Architect
Sitening, LLC

Strategic Open Source: Open Your i™

http://www.sitening.com/
110 30th Avenue North, Suite 6
Nashville, TN 37203-6320
615-469-5150
615-469-5151 (fax)

On Aug 29, 2005, at 4:05 PM, Michael Schmidt wrote:

> Mr. O'Connell,
> Thanks so much for the insights.  Sorry about the basic nature of  
> the question - perhaps a "PostgreSQL for Dummies" book would help me!
> Michael Schmidt
>




^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2005-08-30 15:13 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2005-08-22 00:43 Numerical variables in pqsql statements Michael Schmidt <MichaelMSchmidt@msn.com>
2005-08-29 19:58 ` Thomas F. O'Connell <tfo@sitening.com>
2005-08-30 15:13   ` Thomas F. O'Connell <tfo@sitening.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox