Welcome to CrankyGoblin.Com Sign in | Join | Help

Public Class GeoffAppleby

Inherits Microsoft.VisualBasic.MVP : Implements IBrainFart
Selecting A Random Amount Of Rows

When I wrote the code for my Gaptcha control, I made one pretty silly mistake. I was developing against a SQL2005 database, but CrankyGoblin itself uses SQL2000.

So when I had a SQL statement like this:

select top @someParam col1, col2
from someTable

It would happily select the first @someParam rows from the table - but on SQL2000 this doesn't work. In SQL2000 you must hard code the value when calling TOP, not pass it some variable.

So how do you get around this? Again, thanks to my mate Noonie, I got an answer. I don't know how it works for large amounts, but in Gaptcha's case it's a pretty small amount (9 normally) you can do this:

set ROWCOUNT @someParam
select col1, col2
from someTable
set ROWCOUNT 0

Pretty neat! Thanks Noonie - I was sure that I needed a temp table for this :)

Posted: Tuesday, 17 October 2006 11:22 PM by Geoff Appleby
Filed under: , ,

Comments

lb said:

you learn something new every day! what a cool thing. This could be used for part of paging, without using temp tables.

# October 18, 2006 5:09 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

To submit your comment, click on these pictures:
  • Geoff's bald spot
  • Angry Geoff
  • Geoff's little sister's pussy
Gaptcha Image - No Peeking! Gaptcha Image - No Peeking! Gaptcha Image - No Peeking!
Gaptcha Image - No Peeking! Gaptcha Image - No Peeking! Gaptcha Image - No Peeking!
Gaptcha Image - No Peeking! Gaptcha Image - No Peeking! Gaptcha Image - No Peeking!
Can't recognise the people in these pictures? Look here for a quick introduction.
There's a time limit for you to get your comment submitted before this set of pictures expires. If you think it's been longer than 10 minutes, get some new pictures first (you won't lose what you've typed so far).
Get some new pictures 

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS