By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,903 Members | 1,067 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,903 IT Pros & Developers. It's quick & easy.

Quoting Problem in PLPGSQL

P: n/a
Hi,

i have a problem with quoting in plpgsql. I try to insert dynamically
some conditions into a select statment but for some reason it does not work.

condition := '' AND price > 500 '';

SELECT INTO tmpRec * WHERE xxxxxx || condition || ORDER BY userId ;
What is the correct way to do this ?

Thanks
Alex

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 12 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On Tue, 2003-11-18 at 08:54, Alex wrote:
Hi,

i have a problem with quoting in plpgsql. I try to insert dynamically
some conditions into a select statment but for some reason it does not work.

condition := '' AND price > 500 '';

SELECT INTO tmpRec * WHERE xxxxxx || condition || ORDER BY userId ;
What is the correct way to do this ?
FOR tmpRec IN EXECUTE ''SELECT INTO tmpRec * WHERE xxxxxx '' ||
condition || '' ORDER BY userId'' LOOP
...statements...
END LOOP;
From the manual:

37.6.4. Executing Dynamic Commands
Oftentimes you will want to generate dynamic commands inside
your PL/pgSQL functions, that is, commands that will involve
different tables or different data types each time they are
executed. PL/pgSQL's normal attempts to cache plans for commands
will not work in such scenarios. To handle this sort of problem,
the EXECUTE statement is provided:

EXECUTE command-string;

where command-string is an expression yielding a string (of type
text) containing the command to be executed. This string is fed
literally to the SQL engine.

Note in particular that no substitution of PL/pgSQL variables is
done on the command string. The values of variables must be
inserted in the command string as it is constructed.

--
Oliver Elphick Ol************@lfix.co.uk
Isle of Wight, UK http://www.lfix.co.uk/oliver
GPG: 1024D/3E1D0C1C: CA12 09E0 E8D5 8870 5839 932A 614D 4C34 3E1D 0C1C
========================================
"A Song for the sabbath day. It is a good thing to
give thanks unto the LORD, and to sing praises unto
thy name, O most High." Psalms 92:1
---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 12 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.