MsSQL IsNull and undef



I am having a problem trying to bind an undefined value directly into
an IsNull statement. Here is a test script which I managed to put
together (sorry just learning this whole test script stuff). Basically
when I try to bind a undef directly into the Isnull I get none back, if
I create a SQL variable, bind the undef to that then use that variable
in the select it works.

This is done against SQL 2000 Northwind database.

#!perl -w -I./t

use Test::More;
$| = 1;

use_ok('DBI', qw(:sql_types));
use_ok('ODBCTEST');

# to help ActiveState's build process along by behaving (somewhat) if a
dsn is not provided
BEGIN {
if (!defined $ENV{DBI_DSN}) {
plan skip_all => "DBI_DSN is undefined";
} else {
plan tests => 6;
}
}


#DBI->trace(2);
my $dbh = DBI->connect();
unless($dbh) {
# BAILOUT("Unable to connect to the database ($DBI::errstr)\nTests
skipped.\n");
exit 0;
}

$dbh->{AutoCommit} = 1;
pass("Set Auto commit");
is($dbh->{AutoCommit}, 1, "Auto commit retrieved to what was set");

#### testing a simple select

my $rc = 0;
my $CustomerID;
my $sql="
Declare \@CustomerID varchar(20)
SET \@CustomerID=?
select * from ORders where CustomerID =
isnull(\@CustomerID,'VINET')
";
my $sth = $dbh->prepare(qq{$sql});
$sth->bind_param(1,$CustomerID);
$sth->execute();
my $RowCount=0;
while(my $row = $sth->fetchrow_hashref())
{
$RowCount++;
}
is($RowCount,5, "this works");

my $sql2="
select * from ORders where CustomerID = isnull(?,'VINET')
";
my $sth2 = $dbh->prepare($sql2);
$sth2->bind_param(1,$CustomerID);
$sth2->execute();
$RowCount=0;
while(my $row = $sth2->fetchrow_hashref())
{
$RowCount++;
}
is($RowCount,5, "this doesnt work");
# Test(1);
# clean up
$sth->finish;
exit(0);

__END__

.