my $dbh = DBI->connect($conn_string, $user, $password, { RaiseError => 1, }) || die $DBI::errstr; my $sth = $dbh->prepare(<<__END_SQL__); SELECT stuff FROM some_place WHERE some_condition = 1 __END_SQL__ $sth->execute; my $num_rows = 0; while (my @x = $sth->fetchrow) { $num_rows++; do_something_with(@x); } die "No rows found\n" unless $num_rows; print "Done\n";