Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Using the DBI to return the first ix/i rows

by salvadors (Pilgrim)
on Jan 25, 2001 at 16:07 UTC ( [id://54249]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://54249]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-04-24 07:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found