use DBI; my $dbh = DBI->connect( ... ); # get your database handle my $sql = "select i.name, i._eid, i.num, i.val, m.code from ...". " where i.name = m.code and m.code = ?"; # "?" is a "placeholder" my $sth = $dbh->prepare( $sql ); open( R, "<", $datafile_name ) or die "$datafile_name: $!\n"; while () { chomp; my ( $ref ) = ( /^([^\t]+)/ ); $sth->execute( $ref ); # value passed to execute() fills the placeholder # use one of the DBI methods for getting the query results... } #### "select ... from ... where i.name = m.code and m.code in (select ref from tmp)" #### my %ref; open( R, "<", $datafile_name ); while () { if ( /^([^\t]+)/ ) { $ref{$1} = undef; } } my $dbh = DBI->connect( ... ); my $sth = $dbh->prepare( "select m.code, ... from ... where i.name=m.code"); $sth->execute; my $rows = $sth->fetchall_arrayref; for my $r ( @$rows ) { my ( $mcode, ... ) = @$r; next unless exists( $ref{$mcode} ); # do something ... }