Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

I had a solution working two years ago, with Perl on Solaris 2.6 (Sun SPARC) and SQL-Server 7 on WinNT 4.

Back then I wrote the following documentation:

MS SQL-Server (NT) to Perl (Solaris)


  • Perl interface for MS-SQL-Server
  • Perl is running on the local Sun (Solaris) machine
  • SQL Server is hosted on a remote Windows NT box


The connection between the database and the Perl client is implemented over Sybase/MS native TDS format (Tabular Data Stream). This is different from a ODBC connection because no abstraction layer is put between the client and server; they talk in the native, proprietary TDS format. This approach is somewhat limited as the TDS specs are not published and the FreeTDS implementation is based on reverse engineering.

The advantages are big, however, as no separate driver modules are needed and no TCP ports other than the default SQL-Server port (1433) need to be opened up.

At the Perl side of the chain the DBI interface is used. This interface uses specific database abstraction layers for specific database engines. However, for MS-SQL-Server no DBD driver exists. The Sybase driver can be used instead, as the common TDS implementation appears to be broad enough. See the reference section for more details on DBI and DBD::Database interfaces.

Important: as Sybase is rather different from MySQL you might want to set some database dependent features when calling DBI's connect() method.

Notably, the syb_flush_finish flag is needed to flush any possibly remaining rows when calling finish() on the statement handle. See below for an example program.

Also, take care of the AutoCommit feature, which by default is OFF. All changes on the data must be committed by calling the commit() of the statement handle. MySQL users may want to set AutoCommit to a true value (1).

Text fields are truncated to 4096 bytes by default. As TEXT fields are not handled like BLOB data, setting LongReadLenght has no effect. You should execute the following SQL query instead:

$dbh->do('SET textsize 32000');

Installation instructions:

These instructions are based on

  • FreeTDS (web) version 0.50
  • DBD::Sybase (CPAN) version 0.21

First, make and install the FreeTDS libraries. For MS SQL-Server 7 the TDS protocol version needs to be either 4.2 or 7.0, with 4.2 being much more mature. The documentation and FAQ are quite helpful with this and other topics.

making the FreeTDS package

cd /usr/freetds

./configure --prefix=/usr/freetds --with-tdsver=4.2 \
            --enable-msdblib --enable-dbmfix 
make install

NOTE: you should use GNU make as the default Solaris make appears pretty brain dead.

NOTE 2: don't expect the regression tests with make check and/or make test to run; you'd better skip them. The build process should run without errors and a few warnings, though.

making and installing DBD::Sybase

From the FreeTSD FAQ:

" DBD::Sybase is the recommended option, and yes it does work with MSSQL.
You will need to install the perl DBI module and the FreeTDS package, particularly the CTLib portion.
Set your SYBASE environment variable to /usr/local/freetds and install DBD::Sybase.
Don't worry too much if some of the tests fail.
Do worry if the module doesn't compile.
Make sure you have the most recent version of FreeTDS installed.
You can check the TDS mailing list archives or ask the mailing list for help. "

Important: you should set the SYBASE environment variable before trying to build the interface, as shown on the first line below!

export SYBASE=/usr/freetds

cd /usr/src/DBD-Sybase-0.21

perl Makefile.PL
make install

NOTE: here again make test is likely to fail. Writing your own simple test program is highly recommended!

#!/usr/bin/perl -w
use DBI;

my $SERV = '';
my $USER = 'me_myself';
my $PASS = 'mypassword';

my $dbh = DBI->connect("dbi:Sybase:server=$SERV", $USER, $PASS,
                           {PrintError => 0, syb_flush_finish => 1});
$dbh->do('SET textsize 32000');

my $sth = $dbh->prepare("SELECT TOP 5 id, testcol FROM test ORDER BY id");

while (my $hr = $sth->fetchrow_hashref) {
  print map("$_: $$hr{$_}, ", sort keys %$hr), "\n";



  • Perl Module DBI (distribution on CPAN), the universal Database Interface.
  • The accompanying DBI::FAQ (distribution on CPAN), Frequently Asked Questions, you better read it ;-).
  • Perl Module DBD::Sybase (distribution on CPAN), the DBI driver for Sybase.
  • homepage of the FreeTDS library implementation.
Cheers, Joe

In reply to Re: SQL Server freeTDS walkthrough? by joe++
in thread SQL Server freeTDS walkthrough? by Anonymous Monk

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others making s'mores by the fire in the courtyard of the Monastery: (4)
    As of 2018-05-28 04:21 GMT
    Find Nodes?
      Voting Booth?