You are on the right track. You do not have enough beginning and end. Also, be sure to include the @dhome value. It looks like you started and commented on your third line:
Declare @dhome Tinyint, @bp smallint, @lr smallint, @q smallint // Set @dhome = 1 While(@dhome <= 3) // My attempt to add a loop begin SELECT @lr = MAX(NQdDate), @q = NQd FROM NQdHistory WHERE dhomeId = @dhome GROUP BY NQdDate, NQd SELECT @bd = COUNT(*) FROM bdhome WHERE NQdDate= @lr AND dhomeID= @dhome DELETE FROM ND1 WITH(XLOCK) WHERE dhomeID= @dhome AND NQdDate= @lr UPDATE NQdHistory SET Nbd = @q - @@RowCount - @bp, NBd = @bp WHERE NQdDate= @lr AND dhomeID= @dhome Set @dhome = @dhome +1 //My attempt to end a loop end
If you are familiar with C / C # / C ++, think of T-SQL Begin and End as curly braces { and } if you are more familiar with VB Then and End If . Or more like Pascals Begin and End . You get the idea :)
Adam plocher
source share