Шукати в цьому блозі

вівторок, 26 червня 2012 р.

Запрос к MSSQL на Perl из Linux

Собственно используем FreeTDS, /etc/freetds/freetds.conf:
[orion]
        host = 10.254.103.38
        port = 1433
        client charset = UTF-8
        tds version = 8.0
        text size = 20971520
Устанавливаем tdsodbc, unixodbc и libdbd-odbc-perl:
$ sudo aptitude install tdsodbc libdbd-odbc-perl unixodbc
Настраиваем UnixODBC, /etc/odbcinst.ini:
[FreeTDS]
Description = FreeTDS
Driver      = /usr/lib/odbc/libtdsodbc.so
Setup       = /usr/lib/odbc/libtdsS.so
UsageCount  = 1
Ну вот, теперь пишем запрос в Perl'е:
#!/usr/bin/perl -w

use strict;
use DBI;

my $dbh = DBI->connect('dbi:ODBC:server=orion;port=1433;driver=FreeTDS;tds_version=8.0', 'username', 'password', {PrintError => 0});
die "Unable for connect to server $DBI::errstr" unless $dbh;

my $rc;
my $sth;
    
#$sth = $dbh->prepare('select @@servername');
$sth = $dbh->prepare('SELECT [Caption] AS conn_details, 0 AS cnt_conn_details FROM [OrionDB].[dbo].[Nodes] GROUP BY [Caption] ORDER BY conn_details');
if($sth->execute) {
    while(my $dat = $sth->fetchrow_hashref) {
        foreach my $k (sort keys %{ $dat }) {
            print $k,"=>",${ $dat }{$k},"\n";
        }
    }
}

Немає коментарів: