Perl

There are a few ways to use Perl to connect to a SQL Server using FreeTDS. The first is using DBD::Sybase from Michael Peppler. Despite the name it works for either the range of Sybase servers or Microsoft SQL Server. DBD::Sybase uses the ct-lib API and works well.

There is a DBD::FreeTDS written by Craig Spannring which is a native implementation in Perl, however it has not seen development in quite some time.

A third option is to use DBD::ODBC with the FreeTDS ODBC driver. Brian has gotten this to work fairly well but it is still new, so YMMV.

Lastly, you can use Sybperl. Scripts written against Sybperl will not run against other databases like DBI will, however it will be familiar ground for those used to db-lib or ct-lib.

Building DBD::Sybase

$ cd DBD-Sybase-0.91
$ export SYBASE=/usr/local/freetds
$ perl Makefile.PL
$ make
$ su root
Password: 
$ make install
There will be some output about missing libraries after perl Makefile.PL. These are normal.

The following example will attach to the JDBC server (from the interfaces file) and run a simple query (it can be found in samples/test.pl):

#!/usr/local/bin/perl
#
use DBI;

my $dbh = DBI->connect("dbi:Sybase:server=JDBC", 'guest', 'sybase', {PrintError => 0});

die "Unable for connect to server $DBI::errstr"
    unless $dbh;

my $rc;
my $sth;

$sth = $dbh->prepare("select \@\@servername");
if($sth->execute) {
    while(@dat = $sth->fetchrow) {
		print "@dat\n";
    }
}

Building DBD::ODBC

$ cd DBD-ODBC-0.28
$ export SYBASE=/usr/local/freetds
$ export ODBCHOME=/usr/local
$ export DBI_DSN=dbi:ODBC:JDBC
$ export DBI_USER=guest
$ export DBI_PASS=sybase
$ perl Makefile.PL
$ make
$ su root
Password: 
$ make install

Note: I've used the public JDBC server logins for my configuration here, you should replace these with ones suitable to your environment.

#!/usr/local/bin/perl
#
use DBI;

my $dbh = DBI->connect("dbi:ODBC:JDBC", 'guest', 'sybase', {PrintError => 0});

die "Unable for connect to server $DBI::errstr"
    unless $dbh;

my $rc;
my $sth;

$sth = $dbh->prepare("select \@\@servername");
if($sth->execute) {
    while(@dat = $sth->fetchrow) {
		print "@dat\n";
    }
}
You'll note this is the same program as for DBD::Sybase with the exception of the connect statement, welcome to the magic of DBI!