I have the following table test
Id Code ParentId
1 R O
2 Y O
3 P O
4 O NULL
I need to update test like that :
Id Code ParentId
1 R 4
2 Y 4
3 P 4
4 O NULL
I tried that but it doesn't work , any idea ?
update [dbo].[test]
set [ParentId] =
CASE WHEN [ParentId]='' THEN [Id]
else select top 1 [Id] from [dbo].[PNL] where ParentId=[Code]
End
I got the table test updated
Id Code ParentId
1 R NULL
2 Y NULL
3 P NULL
4 O NULL
[dbo].[PNL]table look like? What error do you get? If no error, what is the resulting data?parentIdwould benull. Why do you want it to be a parent of itself? This makes some queries quite unsafe, as this would be unusual (so normal queries looking for the root would cause infinite recursion).