Skip to content

Instantly share code, notes, and snippets.

@mbeijen
Last active December 28, 2015 20:49
Show Gist options
  • Select an option

  • Save mbeijen/7560268 to your computer and use it in GitHub Desktop.

Select an option

Save mbeijen/7560268 to your computer and use it in GitHub Desktop.
I tried this with DBD::ODBC 1.43 and 1.46_1 on Windows, against SQL Server 2012. Result is here: https://gist.github.com/mbeijen/5a497e05e0739ef93ce9
#!/usr/bin/perl
use strict;
use warnings;
use utf8;
use DBI qw(:utils);
my $dbh =
DBI->connect( 'DBI:ODBC:driver={SQL Server};Server=localhost;Database=test','user','pass')
|| die "Can't connect: $@";
$dbh->do('CREATE TABLE mike( unicode_test NVARCHAR(50) )');
for my $name ( 'Сатурн среди планет', 'some latin1 string' ) {
my $sth = $dbh->prepare('INSERT INTO mike (unicode_test) VALUES ( ? )');
$sth->execute($name);
my @tests = (
{
name => 'SELECT without bind',
statement =>
"SELECT unicode_test FROM mike WHERE unicode_test = '$name'",
},
{
name => 'SELECT N without bind',
statement =>
"SELECT unicode_test FROM mike WHERE unicode_test = N'$name'",
},
{
name => 'SELECT LIKE without bind',
statement =>
"SELECT unicode_test FROM mike WHERE unicode_test LIKE 'Сатурн%'",
},
{
name => 'SELECT LIKE N without bind',
statement =>
"SELECT unicode_test FROM mike WHERE unicode_test LIKE N'Сатурн%'",
},
{
name => 'SELECT with bind',
statement => 'SELECT unicode_test FROM mike WHERE unicode_test = ?',
bind => $name,
},
{
name => 'SELECT LIKE with bind',
statement =>
'SELECT unicode_test FROM mike WHERE unicode_test LIKE ?',
bind => 'Сатурн%',
},
);
for my $test (@tests) {
print "\nexecuting $test->{name}\n";
my $sth = $dbh->prepare( $test->{statement} );
if ( $test->{bind} ) {
$sth->execute( $test->{bind} );
}
else {
$sth->execute();
}
my $result = $sth->fetchall_arrayref;
if ( !@{$result} ) {
print "No results!\n";
next;
}
my $test = data_diff( $name, $result->[0][0] ) || 'OK';
print "Result: $test\n";
}
}
$dbh->do('DROP TABLE mike');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment