Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Regarding the first problem (limiting results to page-size subsets with MS SQL), I use a nested select that I picked up from a DBA much craftier than myself, and I use it in code like the following (using an arbitrary 'parts' table with fields 'partno' and 'custno'):

my $perpage = '25'; my $curpage = $q->param('curpage') || 1; # Assumes $q is a CGI object my $custno = $q->param('custno'); my $offset = $curpage*$perpage; my $orderby = 'partno'; my $totalcount = @{ $dbh->selectrow_arrayref( qq{SELECT count(*) FROM parts WHERE custno = '$custno'} ) }[0]; # Next four lines limit the results of the LAST page's data, in case o +f a short page. my $reccount = $perpage; if (($totalcount - $offset) < 0) { $reccount = $totalcount-$offset+$perpage; } # Now build the select (read inside out) (my $select = <<ENDOFSQL) =~ s/^\s*#.*\n//gm; SELECT * FROM ( SELECT top $reccount * FROM ( SELECT top $offset * FROM parts WHERE custno = '$custno' ORDER BY $orderby DESC ) top_data ORDER BY $orderby ASC ) page_data ORDER BY $orderby DESC ENDOFSQL my $dataref = $dbh->selectall_arrayref($select,{Columns=>{}}) || die ' +Whoops!';

The main select gets everything past the $offset, it's then wrapped in a select that flips the order and gets just the TOP $reccount of the first set, in turn that's wrapped in a last outer select which just flips the order back right-side-up.

Anyone else use something like this?

I know I'm wasting a few cycles doing the $totalcount lookup, but I also use that number to generate the page navigation links. In fact, I might ask for critique of my navLinks() sub here at some point, I think it's kinda nifty.


In reply to Re: Perl DBI MS SQL Question by sedhed
in thread Perl DBI MS SQL Question by peppiv

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-04-24 22:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found