0

I have a table, and I want to select only the single column of row IDs from it, but in a specific order. Then, I want to loop through that column like below:

for (i=0; i<rows.length; i++)
{
  if(i==rows.length-1)
    UPDATE myTable SET nextID = NULL WHERE ID = rows[i]
  ELSE
    UPDATE myTable SET nextID = rows[i+1] WHERE ID = rows[i]
}

I just dont know how to access the results of my select statement with an index like that. Is there a way of doing this in sql server?

3
  • 2
    You want CURSOR, but I strongly suggest to reconsider it and find solution which is SET based, not ROW based. Looping each row (CURSOR) is weapon of last resort, when everything else fails. Commented Aug 27, 2015 at 1:37
  • 2
    Performing update inside a loop is probably the wrong approach. You just want to write a single, good update statement. If you show the select statement that fetches the rows you want to update, you'll be able to ensure you get an accurate answer. Commented Aug 27, 2015 at 1:43
  • What language is this? It looks like PHP? Please tag appropriately. Commented Aug 27, 2015 at 2:04

2 Answers 2

1

Since you didn't provide many details, let's pretend your table looks something like this:

create table MyTable (
  Id int not null primary key,
  Name varchar(50) not null,
  NextId int
)

I want to select only the single column of row IDs from it, but in a specific order

Let's just say that in this case, you decide to order the rows alphabetically by Name. So let's pretend that the select statement that you want to loop through looks like this:

select Id
  from MyTable
 order by Name

That being the case, instead of looping through the rows and attempting to update each row using the pseudo-code you provided, you can replace the whole thing with a single update statement that will perform the exact same work:

with cte as (
  select *,
         NewNextId = lead(Id) over (order by Name)
    from MyTable
)
update cte
   set NextId = NewNextId

Just make sure to adjust the order by clause to whatever your specific order really is. I just used Name in my example, but it might be something else in your case.

Sign up to request clarification or add additional context in comments.

Comments

1

You could use a cursor, or you could use something a bit smarter.

Your example should be able to be written fairly easily along the lines of:

update mytable set nextID = LEAD(id,1) over (order by id)

Lead(id,1) will grab the next id, 1 row ahead in the record set and update the nextID field with it. If it can't find one it will return null. No looping or conditional logic needed!

edit: I forgot the over clause. This is the part that tells it how you would like it ordered for the lead

2 Comments

just a note lead only applies to sql 2014 and above.
Ah, good point. 2012 should work also though: msdn.microsoft.com/en-us/library/hh213125(v=sql.110).aspx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.