Hi, I am a beginner at SQL Server and I have a question about how best to do this.
I have a table that looks like this:
ID Parent Level
1 NULL 0
2 1 1
3 1 1
4 2 2
5 2 2
6 3 2
7 2 2
8 5 4
9 4 3
10 6 3
11 6 3
As you can see, all the entries have a Parent and a Level and the database is organized in a tree structure. There are some entries where the Level is set incorrectly such as entry ID #8. The Parent of 8 is 5 and ID 5 has a level of 2 so the level of 8 should be 3 and not 4. There are many incorrect Level values in my table and I'm not sure how to fix this. So far I have this:
UPDATE myTable
SET level=level-1
FROM myTable
WHERE ???;
I am not sure how to fill in the WHERE part or whether this is the best way to do this. Any suggestions are gladly appreciated.