How do I connect ActiveState ActivePerl on UNIX and Linux to SQL Server?

Use the SQL Server ODBC driver to connect ActiveState ActivePerl to Microsoft SQL Server. The SQL Server ODBC driver is available for 32-bit and 64-bit Linux and UNIX (AIX, HP-UX, and Solaris) platforms.

#!/opt/ActivePerl-5.10/bin/perl -w
#
# Example: Connecting ActivePerl on UNIX and Linux to MSSQL
#

use strict;

use DBI;

# Replace datasource_name with the name of your data source.
# Replace database_user_name and database_password
# with the SQL Server database user name and password.
my $data_source = q/dbi:ODBC:datasource_name/;
my $user = q/database_user_name/;
my $password = q/database_password/;

# Connect to the data source and get a handle for that connection.
my $dbh = DBI->connect($data_source, $user, $password)
    or die "Can't connect to $data_source: $DBI::errstr";

# This query generates a result set with one record in it.
my $sql = "SELECT 1 AS test_col";

# Prepare the statement.
my $sth = $dbh->prepare($sql)
    or die "Can't prepare statement: $DBI::errstr";

# Execute the statement.
$sth->execute();

# Print the column name.
print "$sth->{NAME}->[0]\n";

# Fetch and display the result set value.
while ( my @row = $sth->fetchrow_array ) {
   print "@row\n";
}

# Disconnect the database from the database handle.
$dbh->disconnect;
  1. Download the ActivePerl distribution from the ActiveState web site.
  2. Copy the distribution file to your Perl machine, unpack and cd into the directory created by unpacking the file. For example:
    $ gunzip ActivePerl-5.10.0.1004-i686-linux-glibc-2.3.2-287188.tar.gz
    $ tar -xvf ActivePerl-5.10.0.1004-i686-linux-glibc-2.3.2-287188.tar
    $ cd ActivePerl-5.10.0.1004-i686-linux-glibc-2.3.2-287188
  3. As root, install ActivePerl:
    # sh install.sh
  4. Download the SQL Server ODBC driver for your ActivePerl client platform.

    If the SQL Server ODBC driver is not currently available for your platform, check the list of SQLConnect function (SQLConnectW). This is the method that ODBC defines for reporting Unicode support to a Driver Manager.

    The DBD::ODBC module included in the ActivePerl distribution is a non-Unicode version, and does not make wide function calls. However, the iODBC Driver Manager detects that the SQL Server ODBC driver supports wide functions and uses them. The iODBC Driver Manager passes 4 byte Unicode characters to the wide functions, instead of the standard 2 byte characters, which the SQL Server ODBC driver expects. Because the strings passed to SQLConnectW are not in the expected format, the connection fails.

    To work around this, use the non-Unicode version of the driver, which does not support wide function calls (and therefore does not export SQLConnectW). To do this, edit the Driver in your data source. For example:

    [MY_SQL_SERVER_ODBC_DRIVER_DSN]
    Driver          = /usr/local/easysoft/sqlserver/lib/libessqlsrv_a.so
    .
    .
    .
  5. You can replace the DBD::OBDC module included in the ActivePerl distribution with one built against the unixODBC Driver Manager. For example:
    # cd /opt/ActivePerl-5.10/lib/auto/DBD
    # mkdir ODBC.original
    # mv ODBC/* ODBC.original
    $ cd /tmp
    $ tar -xvf DBD-ODBC-1.17.tar
    $ cd DBD-ODBC-1.17
    $ ODBCHOME=/usr/local/easysoft/unixODBC
    $ DBI_DSN="dbi:ODBC:MY_SQL_SERVER_ODBC_DRIVER_DSN"
    $ DBI_USER="my_domain\my_user"
    $ DBI_PASS=my_password
    $ export ODBCHOME DBI_DSN DBI_USER DBI_PASS
    $ /opt/ActivePerl-5.10/bin/perl Makefile.PL
    $ make
    $ make test
    # make install

    All Easysoft ODBC driver distributions include the unixODBC Driver Manager, and unixODBC is supplied with many Linux distributions.

    For more information about building DBD::ODBC, refer to the README file in the DBD::ODBC distribution and Enabling ODBC support in Perl with Perl DBI and DBD::ODBC.

  6. Further information