Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re: Querying Select Number of Rows

by chipmunk (Parson)
on Jun 05, 2001 at 08:08 UTC ( #85688=note: print w/replies, xml ) Need Help??

in reply to Querying Select Number of Rows

Why can't you use that if fewer than 11 rows are returned? That code will work just fine, regardless of the number of rows.

You want to select the oldest 11 rows (after a certain date), without ordering by date and time... But if you don't order by date and time, how do you know which are the oldest??

To fill out your code a little more:

#!perl -w use strict; use DBI; # note: must be uppercase my $dbh = DBI->connect("dbi:ODBC:burlee", "username", "password") or die "Can't connect to database: $DBI::errstr\n"; { # $sth is a static variable for get_sport() # it's private to get_sport(), and keeps its value between calls my $sth; # get_sport($sport, $import) # returns a list of array references sub get_sport { my ($sport, $import) = @_; my $path = "d:/text/time/$sport.txt"; open(TEXT, $path) or die "Can't open $path: $!\n"; my $date = <TEXT>; chomp $date; close(TEXT); # prepare, if it wasn't prepared before $sth ||= $dbh->prepare(<<" EndOfSQL"); select * from DATA_DB_ENTRY__ASTROS_STAGING where TS_DATE >= ? and IMPORTACE = ? order by TS_DATE asc, TS_TIME asc EndOfSQL $sth->execute($date, $import); my @rows; while (my(@row) = $sth->fetchrow_array()) { push @rows, [@row]; if (@rows == 11) { $sth->finish(); last; } } return @rows; } }
The most important thing here is adding rows to the results list one at a time, and stopping if you get to 11.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2022-01-25 10:24 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (66 votes). Check out past polls.