Hi all Suppose you have a table like create table temp_one ( catid int, parentid int d int )
lets assume we have the following inserts like catid parentid d 1 0 2 1 3 2 4 3 5 4
Here comes the question With a cursor how can i update d field with the catid having parentid = 0 Output should be like catid parentid d 1 0 1 2 1 1 3 2 1 4 3 1 5 4 1
I need cursor cause the depth is not static it is dynamic also In your update query i have to assume that there is only one 0 parentid but there wont be only one parentid having value 0. there will be many parentid with 0 like
You mean you want d to be the root of the tree? Assuming d starts off as null.
update tbl set d = catid where parentid = 0 while @@rowcount > 0 update tbl set d = p.d from tbl c join parent p on p.catid = c.parentid where c.d is null and p.d is not null
========================================== Cursors are useful if you don't know sql. DTS can be used in a similar way. Beer is not cold and it isn't fizzy.