## @column_names contains column names $sth = $dbh->prepare("SELECT ? FROM employees"); foreach my $column_name (@column_names){ $sth->execute($column_name) or error_somehow(); while($row = $sth->fetchrow_arrayref()){ print "Value in column $column_name: ".$row->[0]; } }