$incname =~ s/\'/\'\'/g; # change syntax for embedded quotes in Postgres $sql = sprintf( 'select count(le_id) ' . 'from legal_entity ' . 'where le_corid = 586 ' # this routine is for data from a particular registrar . 'and le_lesid = %s ' . 'and le_inc_name = %s', $lesid, "'" . $incname . "'" ); $sth = $dbh->prepare($sql); $sth->execute; my ($count_le) = $sth->fetchrow_array; if ($count_le) { print "$. - existing incname\n"; next LINE; }