Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VDLlZ-00031d-5E for pgsql-interfaces@arkaria.postgresql.org; Sat, 24 Aug 2013 21:53:05 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1VDLlY-0007b2-Kz for pgsql-interfaces@arkaria.postgresql.org; Sat, 24 Aug 2013 21:53:04 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VDLlX-0007av-J5 for pgsql-interfaces@postgresql.org; Sat, 24 Aug 2013 21:53:03 +0000 Received: from brama.com ([208.67.225.160]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VDLlP-00057P-8l for pgsql-interfaces@postgresql.org; Sat, 24 Aug 2013 21:53:01 +0000 Received: from brama.com (localhost [127.0.0.1]) by brama.com (8.14.4/8.14.4) with ESMTP id r7OLqoKt008835; Sat, 24 Aug 2013 17:52:50 -0400 Received: from localhost (pyz@localhost) by brama.com (8.14.4/8.14.4/Submit) with ESMTP id r7OLqi7O008821; Sat, 24 Aug 2013 17:52:49 -0400 Date: Sat, 24 Aug 2013 17:52:44 -0400 (EDT) From: Max Pyziur To: Daniel Verite cc: pgsql-interfaces@postgresql.org Subject: Re: binding a variable to NULL in perl-DBD In-Reply-To: Message-ID: References: User-Agent: Alpine 2.03 (LRH 1266 2009-07-14) MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed X-Pg-Spam-Score: -2.8 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-interfaces Precedence: bulk Sender: pgsql-interfaces-owner@postgresql.org On Thu, 22 Aug 2013, Daniel Verite wrote: > Max Pyziur wrote: > >> I'm trying to determine how to pass "NULL" to a variable, specifically in >> the conditional section of a SQL statement: >> >> SELECT moo >> FROM foo aa >> WHERE field1 = ? >> AND field2 = ? > > Perl's undef is used to pass NULL as a literal but field=NULL > will never be true. > >> SELECT moo >> FROM foo aa >> WHERE field1 = 'goo' >> AND field2 IS NULL > > You may use: > WHERE field1 = ? > AND field2 IS NOT DISTINCT FROM ? Much thanks for your help. The way that I'm making this work is that on the command line for the argument of interest I use the word NULL, then in the perl code I have: if ($FIELD2_CODE eq "NULL") { undef($FIELD2_CODE); } and then in the SQL: "AND field2 IS NOT DISTINCT FROM ?" seems to work Do I have a correct understanding of Postgres SQL's vernacular and application here? > which conveys the idea that field2 must be equal to the value passed, > and works as expected with both non-NULL literals and NULL (undef). > > Best regards, > Thank you again, Max Pyziur pyz@brama.com -- Sent via pgsql-interfaces mailing list (pgsql-interfaces@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-interfaces