Loop through non-integer rows using SQL

Posted by Jesse on Stack Overflow See other posts from Stack Overflow or by Jesse
Published on 2010-06-10T22:01:22Z Indexed on 2010/06/10 23:32 UTC
Read the original article Hit count: 149

Filed under:
|

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.

© Stack Overflow or respective owner

Related posts about sql

Related posts about sql-server