Friday, January 20, 2012

SQL : Row-By-Row Processing Without Cursor

Row-By-Row Processing Without Cursor

Here is the Logic.....
--Create a Table Variable and Variable Counter

DECLARE @InitialTable TABLE(RowNo INT IDENTITY(1,1) NOT NULL PRIMARY KEY CLUSTERED, Field1 int, Field2 Int)
DECLARE @IncrementalCounter INT 'This variable is declare for Loop

--All values are inserted into Your Table Variable ........
Insert into @InitialTable (field1, Feld2)
Select A1, A2 from Your_Table Where Your_Condition
----------------------------------------------------------
--Initialize the Counter
SET @IncrementalCounter = 1
--Condition of Checking the No of records to be traverse
while @IncrementalCounter <= ISNULL((SELECT COUNT(RowNo) FROM @InitialTable),0)
BEGIN
--Here we set the variable value to the @variable
SELECT @Variable = Field1 FROM @InitalTable where RowNo = @IncrementalCounter

SET @IncrementalCounter = @incrementalCounter +1
END


Reference:

http://www.sqlservercentral.com/Forums/Topic327280-319-1.aspx

 
doggy steps
doggy steps