title: Perl DBI dynamic fetchrow while loops
body: <p>I'm trying to pass table names to a sub that gets all the field names of that table, stores them into an array, and then uses that array in conjunction with the fetchrow of another sql query to display the data in those fields.  Here's the code I have now:<br><br></p>
  
  <p>Examples of sub calls with table names as the parameter:</p>
  
  <pre><code>shamoo("reqhead_rec");
  shamoo("approv_rec");
  shamoo("denial_rec");
  </code></pre>
  
  <p>shamoo sub:</p>
  
  <pre><code>sub shamoo
  {
      my $table = shift;
      print uc($table)."\n=====================================\n";
  
      #takes arg (table name) and stores all the field names into an array
      $STMT = &lt;&lt;EOF;
      select first 1 * from $table
      EOF
  
      my $sth = $db1-&gt;prepare($STMT);$sth-&gt;execute;
  
      my ($i, @field);
      my $columns = $sth-&gt;{NAME_lc};
      while (my $row = $sth-&gt;fetch){for $i (0 .. $#$row){$field[$i] = $columns-&gt;[$i];}}
  
      $STMT = &lt;&lt;EOF;
      select * from $table where frm = '$frm' and req_no = $req_no
      EOF
      $sth = $db1-&gt;prepare($STMT);$sth-&gt;execute;
      $i=0;
      while ($i!=scalar(@field))
      {
      #need code for in here...
      }
  }
  </code></pre>
  
  <p>I am looking for a way to turn this nto something that doesn't have to be explicitly defined....
  <br></p>
  
  <pre><code>my ($frm, $req_no, $auth_id, $alt_auth_id, $id_acct, $seq_no, $id, $appr_stat, $add_date, $approve_date, $approve_time, $prim);
  while(($frm, $req_no, $auth_id, $alt_auth_id, $id_acct, $seq_no, $id, $appr_stat, $add_date, $approve_date, $approve_time, $prim) = $sth-&gt;fetchrow_array())
  </code></pre>