Setting up ODBC connection to SQL Server in on linux/perl


Useful info from http://www.idevelopment.info/data/MSSQL/DBA_tips/Programming/PROG_2.shtml

Specifically – Install DBI / FreeTDS / DBD-Sybase

Configure FreeTDS

At this point, all software components and modules are installed. The final step is to configure FreeTDS to talk to your SQL Server 2000 database! FreeTDS uses a configuration file called freetds.conf. This file can be found in the FreeDS installation directory under /etc. In this article, the file would be located at /usr/local/freetds/etc/freetds.conf. This file contains several configuration examples (some commented out) and it is very possible to modify one of them to reflect your server’s information.

Here is an example entry I made:

[melody]
	host = melody.idevelopment.info
	port = 1433
	tds version = 8.0

The name of my SQL Server in the above example is melody.idevelopment.info and is running its database service on the default port of 1433. Notice that I have also specified the tds version of 8.0.

A Simple Example Now let’s take a look at our new configuration in action by running a couple of example Perl scripts.

Query the Server Name
#!/usr/local/bin/perl
#
use DBI;

my $user   = "sa";
my $passwd = "mypassword";

my $dbh = DBI->connect("DBI:Sybase:server=melody", $user, $passwd, {PrintError => 0});

unless ($dbh) {
    die "Unable for connect to server $DBI::errstr";
}

my $sth;

$sth = $dbh->prepare("select @@servername");

if ($sth->execute) {
    while(@dat = $sth->fetchrow) {
      print "SERVER IS: @datn";
    }
}

exit(0);

$ perl queryServer.pl
SERVER IS: MELODY

Leave a Reply

Your email address will not be published.