Re: binding a variable to NULL in perl-DBD

From: "Daniel Verite" <daniel(at)manitou-mail(dot)org>
To: "Max Pyziur" <pyz(at)brama(dot)com>
Cc: pgsql-interfaces(at)postgresql(dot)org
Subject: Re: binding a variable to NULL in perl-DBD
Date: 2013-08-22 20:48:55
Message-ID: ceef0d9a-f613-4196-bc9c-b5e90ebcd35e@mm
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-interfaces

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

In response to

Responses

Browse pgsql-interfaces by date

  From Date Subject
Next Message Max Pyziur 2013-08-24 21:52:44 Re: binding a variable to NULL in perl-DBD
Previous Message Max Pyziur 2013-08-21 21:06:26 binding a variable to NULL in perl-DBD