Skip navigation.
Home
Your source for Perl tips, howto's, faq and tutorials
( categories: )

To access Microsoft SQL server from unix, use the DBD:Sybase module.

In order to access SQL Server 2000 (and later) databases, you have to be sure that the DBD:Sybase module was built with the FreeTDS libraries.

Several *nix distributions have ready to install packages (for example, in Debian, you just need to install the packages 'libdbd-sybase-perl' and 'freetds-dev'); in case you need to install from sources, you need to do the following:

  1. install FreeTDS

    Get the package from www.freetds.org, unpack it and execute make and then make install

  2. set the environment variable SYBASE

    the environment variable should point to the location of FreeTDS (usually /usr/local/freetds):

    SYBASE=/usr/local/freetds
    export SYBASE

  3. install DBD::Sybase

    Get the package from cpan.org, unpack it and execute perl Makefile.PL (don't worry about testing errors here), then make and make install

Example:

#!/usr/bin/perl
 
#-- open database
use DBI;
my $dbh = DBI->connect('DBI:Sybase:server=192.168.0.1','user','password') or die $DBI::errstr;
$dbh->do("use Northwind");
 
#-- sample query
my $rows = $dbh->selectrow_array("SELECT COUNT(*) FROM Orders");
print "Orders has $rows records\n" if ( defined $rows );


Additional Details

After a few days I got freeTDS to work on openSUSE v11.0 (nov2008) by:

1) In unpacked source dir: /include/cspublic.h about line 450 add:

#define BLK_VERSION_120 BLK_VERSION_100
#define BLK_VERSION_125 BLK_VERSION_100
#define BLK_VERSION_150 BLK_VERSION_100

Note that Mac OS people, linux people, etc. have encountered this issue. This does not seem to affect performance...

2) Then compile up as...

run./configure --with-tdsver=7.0 --prefix=/usr/local/freetds
make
make install

3) Test TDS (separate from perl)
Note mySQLhost is the name specified in brackets [] in /usr/local/freetds/etc/freetds.conf and NOT an IP address or FQDN.

From a bash prompt: tsql -S mySQLhost -U sa
From the TDS prompt: select top 5 * from master.dbo.spt_values
From the TDS prompt: go

You should see a short dump of SQL engine settings.

4) cpan DBD::Sybase
If you do 1 and 2, this actually makes properly.

Happy perl-ing!