Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

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
Node Status?
node history
Node Type: note [id://85688]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (2)
As of 2017-08-22 10:14 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (333 votes). Check out past polls.