T-SQL Update with aggregate

JaedenRuiner

Well-known member
Joined
Aug 13, 2007
Messages
340
Programming Experience
10+
Okay,

I want to Update a Series of Items continual until the sum of a column is greater than equal to a value.
Currently I am Doing this:
(Be Kind this is VERY Psuedo code)
VB.NET:
Expand Collapse Copy
SumID = CurrentSumID
while itemID = CurrentItem
  if ExecScalarSQL("Select Sum(MyTable.Quantity) from mytable where mycolumn=SumID") > X then
    SumID = AddNewSumID()
  end if
  sql = "Update MyTable set MyColumn=SumID where Item=ItemID"
  execsql(sql)
  ItemID = NextItemID(myTable)
end while

Basically I loop through all my rows in MyTable and sum up the quantity column. If I am less than my desired amount, I update those few rows that equate to the current Item ID. Is there a Way that I can Execute a Single UPDATE statement that would automatically select as many of the items from the table that meet the WHERE criteria, and update them while the Sum of a quantity column is less than a value?

Thanks
 
Back
Top