#...more code above, all variable previously declared and strict is also been used. push @all_names, $names; push @email, $email; push @local, $local; push @year, $year; } #end of foreach. #Getting data my $dbh = DBI->connect("DBI:ODBC:$myserver",$u, $p) || print "Connect fail: $!"; my $vals = join("," ,map { $dbh->quote($_) } @all_names ); my $sql = "SELECT DISTINCT name, email, addr, location FROM my_users WHERE year = $year /* need all the values from @year */ AND email = $email /* need all the values from @email */ AND location = $local /* need all the values from @local */ AND name IN ($vals) /* thats how I need */ ";