Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2002
    Posts
    1

    Unanswered: Walk through a table one record at a time without using a cursor

    Hi, I'm a newbie in SQL Server 7.0, can someone tell me how do I walk through a table one record at a time without using a cursor please.

    Greatly appreciated.

  2. #2
    Join Date
    Oct 2001
    Location
    England
    Posts
    426
    You can use the primary key - this is for an ID but will work on any primary key.

    declare @id int, @maxid int
    select @id = 0, @maxid = max(id) from tbl

    while @id < @maxid
    begin
    select @id = min(id) from tbl where id > @id

    select * from tbl where id = @id
    end

    This also works with a filter - just include it in the selects.

    You can also create a temp table with the records and with an identity on it. This can often be more efficient and clearer as you can preprocess all the records before executing the loop.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •