Loop through non-integer rows using SQL
- by Jesse
I know how to accomplish my task with .NET, but I wanted to do this just in SQL.
I need to loop through all of the rows where the primary key is somewhat arbitrary. It can be a number or a series of letters, and probably any number of unusual things.
I know I could do something like this...
DECLARE @numRows INT
SET @numRows = (SELECT COUNT(pkField) FROM myTable)
DECLARE @I INT
SET @I = 1
WHILE (@I <= @numRows)
BEGIN
--Do what I need to here
SET @I = @I + 1
END
...if my rows were indexed in a contiguous fashion, but I don't know enough about SQL to do that if they're not. I keep coming across the use of "cursors," but I come across just as much reading about avoiding cursors.
I found this SO solution but I'm not sure if that's what I'm needing?
I appreciate any ideas.