Reputation: 1673
I'm trying change existing schema to add new column.
Here is my case - http://sqlfiddle.com/#!3/06c58/1
Value of Order
column is int, which should represent order of items on UI from 1 to COUNT(Id)
if I'm grouping TemplateId
.
I see 2 possible solutions - make Order
calculated field or use Cursor to update it.
Which one is better? And if update using cursor win -
May I know how to have such update ?
Upvotes: 0
Views: 81
Reputation: 21757
One way I can think of it is this:
You won't need to directly deal with cursors to do this, and the operation will be atomic in a way, since it's wrapped in a transaction block.
The update itself can be done something like this:
;with ord as
(Select id, row_number() over (partition by templateid order by selectedfieldid) rn from yourtable)
Update dbo.titlefields
Set order = rn - 1 --ROW_NUMBER() starts at 1 but we want to start at 0
From dbo.titlefields t inner join
ord x on t.id = x.id
Please let me know if I have understood your requirement correctly, and I will update my answer if that is not so.
Upvotes: 1