to make perl properly handle utf-8 strings inserted and retrieved from ms sql database, freetds config must be used (the configuration with only odbc.ini is possible to make perl connect to a database, but i could not find any way to make it work with utf)

for this purpose, the entry in odbc.ini should contain “Servername”, which points to an entry in the freetds.conf. the sample configuration may look as following:

#/etc/odbc.ini

[JA223ODBC]
Description = Just Another 223 ODBC entry
Driver = FreeTDS
Servername = MYSERV
Database  = mydatabase

#/usr/local/etc/freetds.conf

[MYSERV]
host = 192.168.1.223
port = 1433
tds version = 8.0
client charset = UTF-8

#perl script connection string:

my $data_source = q/dbi:ODBC:JA223ODBC/; # DSN string from /etc/odbc.ini
my $user = q/username/;
my $password = q/password/;
my $dbh = DBI->connect($data_source, $user, $password, {RaiseError => 0, PrintError => 1}) or die "Can't connect to $data_source: $DBI::errstr"

tips:

- use tds dump for debugging. for this, set the TDSDUMP env variable and try to connect; read debug info from the dump file:

export TDSDUMP=/path/to/tdsdump.txt

- use tsql to debug connection. in another terminal, set the TDSDUMP variable, run

tsql -S MYSERV -U username -P password

then exit, and compare information from the tsql’s dump with the script’s dump

- be careful with config files; freetds can read from /usr/local/etc/freetds or /etc/freetds.conf depending on it’s installation configuration, but will definitely use only $HOME/.freetds.conf if it exists

- and do not forget to enclose your utf-8 strings in N” in queries, otherwise ms sql server will not understand it’s utf:

select stringID from stringtable where string = N'utf-8 ąęćźś string';