TheDugglerrr 0 Newbie Poster

Hi :)

Im not an SQL n00b, but I'm no SQL guru...

I'd like to work out the SQL code required to achieve the following goal in pseudocode:

Grap a record
Using info from that record, add (the width) to a running total.
When the running total exceeds a certain value, stop grabbing records.

Here's a snippet of code to demonstrate the functionality that is required:

------------------------------------------
ImageID ImageWidth      ImageBLOB
------------------------------------------
1       60              %E$Y^HYS^YHU%^TRHT
2       60              O*&^G&*^OGBV&VB^^&
3       120             YE%$CC%$EYYEC%$C%$
4       200             %E$Y^HYS^YHU%^TRHT
5       60              O*&^G&*^OGBV&VB^^&
6       120             YE%$CC%$EYYEC%$C%$
------------------------------------------
Dim CurrentTotalWidth     As Integer =   0
Dim MaxWidth              As Integer = 240
Dim MaxWidthExceeded      As Integer =   0
DO
  IF CurrentTotalWidth > MaxWidth Then 
    MaxWidthExceeded &= MaxWidthExceeded + 1  
  END IF
  SELECT [ImageID], [ImageWidth], [ImageBLOB], FROM [tblImages]
  CurrentTotalWidth = CurrentTotalWidth + [ImageWidth]
LOOP UNTIL MaxWidthExceeded = 5

I don't assume that the code I have submitted is the most efficient yaddah yaddah yaddah, I'm open to suggestions to make things efficient... maybe a SELECT TOP 10 ... so that im running one query instead of ten ? etc etc

Anyways,
Thanks in advance,
Douglas