#!/usr/bin/perl use strict; use warnings; use DBI; use Data::Dumper; # Warning, untested as I don have ODBC on this laptop # I use $dbc to make the line wrap nicer on the monks ;) my $dbc ='dbi:ODBC:DSN=SqlExpress;UID=USER;PWD=pass;Regional=No;'; my $dbh = DBI->connect($dbc) or die "Can't connect: $!"; $dbh->{'LongReadLen'} = 40000; # I took out crcp_cod_grupo from the select as its already known in the WHERE my $sql = ' SELECT crcp_cod_prato'; $sql.= ' FROM REST.dbo.CRCP_PRATO '; $sql.= ' WHERE crcp_cod_grupo = "?"'; # ? is placeholder for bind variable my $sth = $dbh->prepare($sql); # lets look at a few different grupos, this is where the # bin variable gets used. This is more efficient than doing a # prepare, execute for each grupo. for my $crcp_cod_grupo (qw(A B C D E)) { $sth->execute($crcp_cod_grupo); while (my @row = $sth->fetchrow_array) { print "Grupo: $crcp_cod_grupo Prato: $row[0]\n"; } } # I leave the hash version as an excercise for the OP. $dbh->disconnect;