How-To use MS SQL with Perl: FreeTDS und unixODDC

I had to connect from Linux (a SuSE 9.3 Box at work) to a MSSQL Database with ODBC.
All documentation I found were not really usable or the solution was to complicated. Here is how I solved it. Perhaps it helps someone.

You need the unixodbc and unixodbc-devel paket from SuSE.

# installing FreeTDS
$ wget ftp://ftp.ibiblio.org/pub/Linux/ALPHA/freetds/stable/freetds-stable.tgz
$ tar xvfz freetds-stable.tgz
$ cd freetds-$version/
$ ./configure --with-unixodbc=/usr
$ make
$ sudo make install

# configuration of FreeTDS
$ sudo vim /usr/local/etc/freetds.conf
---schnippp---

# A typical Microsoft SQL Server 2000 configuration

[hostdescription-mssql]
  host = full.qualified.domain.name.tld
  port = 1433
  tds version = 8.0
---schnapp---

# configuration of unixODBC
$ sudo vim /etc/unixODBC/odbc.ini
---schnipp---

[system_dsn_name]
Driver          = /usr/local/lib/libtdsodbc.so
Description     = my description of this service
Trace           = 1
Servername      = hostdescription-mssql
Database        = eventlogs
---schnapp---
# Description of some entrys in odbc.ini:
#[system_dsn_name] = System DSN on the MSSQL server
#Servername = servernamedescription in freetds.conf
#Database = Database on the MSSQL Server you want to open

# Tests. Both have to work!
$ isql -v system_dsn_name user_name pass_word
$ tsql -Shostdescription-mssql -Uuser_name

I found the following Script somewhere to connect to the database with perl to get a more verbose erroroutput on failures.

---schnipp---

#!/usr/bin/perl -w
# This Script needs the perl-DBI and perl-DBD-ODBC Package

use strict;
use warnings;
use DBI;

# Configuration
my $user = "my_username";
my $password = "my_password";

my $dbh = DBI-> connect('dbi:ODBC:EVENTLOG', $user, $password, {PrintError => 0,
 RaiseError =>0});
if (!$dbh) {
  print "I will print all the errorshit!";
  print "$DBI::err\n$DBI::errstr\n$DBI::state";
} else {
  $dbh->disconnect if ($dbh);
}
---schnapp---

uhrig.eu.org/
$Id: index.html,v 1.6 2008-03-28 14:10:37 volker Exp $