Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WtzkN-0000w5-Hx for pgsql-sql@arkaria.postgresql.org; Mon, 09 Jun 2014 13:36:23 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1WtzkM-0000tB-Kd for pgsql-sql@arkaria.postgresql.org; Mon, 09 Jun 2014 13:36:22 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1WtzkJ-0000lQ-4V for pgsql-sql@postgresql.org; Mon, 09 Jun 2014 13:36:19 +0000 Received: from sam.nabble.com ([216.139.236.26]) by makus.postgresql.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1WtzkE-0004QF-KL for pgsql-sql@postgresql.org; Mon, 09 Jun 2014 13:36:17 +0000 Received: from [192.168.236.26] (helo=sam.nabble.com) by sam.nabble.com with esmtp (Exim 4.72) (envelope-from ) id 1WtzkD-0007Xh-Ss for pgsql-sql@postgresql.org; Mon, 09 Jun 2014 06:36:13 -0700 Date: Mon, 9 Jun 2014 06:36:13 -0700 (PDT) From: David G Johnston To: pgsql-sql@postgresql.org Message-ID: <1402320973871-5806502.post@n5.nabble.com> In-Reply-To: <1402300317130-5806470.post@n5.nabble.com> References: <1402300317130-5806470.post@n5.nabble.com> Subject: Re: Parameterized Query MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: 2.2 (++) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org 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