Thread: binding a variable to NULL in perl-DBD
Greetings, 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 = ? Sometimes at the command line, when running the perl-DBD script, I would like the select statement to reflect: SELECT moo FROM foo aa WHERE field1 = 'goo' AND field2 IS NULL Much thanks in advance, Max Pyziur pyz@brama.com
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 ? 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, -- Daniel PostgreSQL-powered mail user agent and storage: http://www.manitou-mail.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