#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2009
    Posts
    38
    Rep Power
    6

    Set-based approach to looping through table


    Is there a set-based approach to achieving the following: (At the moment, I am doing this with a cursor which is probably not the best option.)

    The target table (myTable) that I wish to loop through has an integer column (myColumn). What I am trying to do here is to keep a running total of the values in this column. If at the end of the loop, the total = 0, then <do something> Else <do something else>.

    My question: Is it possible to get the running total with a set-based approach?

    TIA!
  2. #2
  3. SQL Consultant
    Devshed Supreme Being (6500+ posts)

    Join Date
    Feb 2003
    Location
    Toronto Canada
    Posts
    27,244
    Rep Power
    4279
    Code:
    SELECT SUM(mycolumn) AS total
      FROM daTable
    and then do your IF logic based on total value
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

IMN logo majestic logo threadwatch logo seochat tools logo