in reply to Using the DBI to return the first ix/i rows

I need to return the first x rows of a 'select' statement

The most obvious way is to put a 'LIMIT x' in your SELECT.

But if for some reason you can't do that, then use something like:

my $sth = $dbh->prepare($sql); $sth->execute; $sth->bind_columns(\my ($col1, $col2 ... $coln)); my $count = 0; while ($sth->fetch) { [$col1 .. $coln will be populated with the values in that row] last if ++$count == 10; }

Tony

Replies are listed 'Best First'.
Re: Re: Using the DBI to return the first ix/i rows
by eg (Friar) on Jan 25, 2001 at 16:36 UTC

    In case you're using Oracle: you don't have a LIMIT SQL command (go figure), so you need to use ROWNUM instead:

    SELECT * FROM table WHERE ROWNUM < 10;

      Sybase doesn't have either of those mechanisms. You need to do it in two steps.

      set rowcount 10 select * from table
      --
      <http://www.dave.org.uk>

      "Perl makes the fun jobs fun
      and the boring jobs bearable" - me