agora inbox for pgsql-sql@postgresql.org
help / color / mirror / Atom feedFrom: David G Johnston <david.g.johnston@gmail.com>
To: pgsql-sql@postgresql.org
Subject: Re: Parameterized Query
Date: Mon, 9 Jun 2014 06:36:13 -0700 (PDT)
Message-ID: <1402320973871-5806502.post@n5.nabble.com> (raw)
In-Reply-To: <1402300317130-5806470.post@n5.nabble.com>
References: <1402300317130-5806470.post@n5.nabble.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-sql>
frankliu747 wrote
> I have a query that works on sql sql server or oracle that do a
> parameterized query, I would like to move this to postgreSQL but I'm just
> not sure how to get it done. This is the query
> select * from quest q
> left join ask a on q.ask_id=a.id
> where q.createtime>=:StartDate and q.createtime<=:EndDate
> and a.asktime>=:StartDate and asktime<=:EndDate
>
> i need to define parameters in the WHERE clause to build dynamic
> SELECT,like ":StartDate" may be repeat more than one time.
>
> This works great on SQL Server or oracle but not on postgreSQL. Any help
> would be appreciated.
>
> and i use sql server reporting service use odbc connect postgres 9.34
The main SQL executor does not support named parameters, though psql does
since it has its own pre-parse step before sending the query to the server.
The typical way of doing this in PostgreSQL is to create a table returning
function with as many arguments as you have parameters. Within the function
body you can reference the input arguments repeatedly.
CREATE FUNCTION do_query(startdate date, enddate date)
RETURNS TABLE (col1 text,col2 date)
AS $func$
SELECT col1, col2 FROM tbl WHERE (col2 BETWEEN startdate AND enddate) AND
(col3 BETWEEN startdate AND enddate);
$func$
LANGUAGE SQL
;
Note that you do not use any special prefix to refer to the arguments in the
query.
In the client you call the function, with, parameters, using the following
query:
SELECT col1,col2 FROM do_query(?,?);
The documentation on CREATE FUNCTION as well as both the SQL and pl/pgsql
languages will be of great assistance on this topic.
David J.
--
View this message in context: http://postgresql.1045698.n5.nabble.com/Parameterized-Query-tp5806470p5806502.html
Sent from the PostgreSQL - sql mailing list archive at Nabble.com.
--
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql
Message-ID: <1402320973871-5806502.post@n5.nabble.com>
Permalink: ../1402320973871-5806502.post@n5.nabble.com/
Also on: postgresql.org/message-id/1402320973871-5806502.post@n5.nabble.com
reply
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: david.g.johnston@gmail.com
Subject: Re: Parameterized Query
In-Reply-To: <1402320973871-5806502.post@n5.nabble.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox