Renumber keys in a table using sub-query
I have a table that contains a list of deparments. The key ('code' in the sample) is just an assigned 开发者_Go百科number. Unfortunately, this table now needs to accomodate some new software that comes with pre-defined deparment codes. Of course, these defined codes collide with the existing codes. I want to take the existing keys and increase each one by 200 to move them out of the collision space. I tried this query:
update [TEST-PH].[dbo].[Emp_PosDepartments]
set Code = (select code + 200 from [TEST-PH].[dbo].[Emp_PosDepartments] o where o.Code = Code)
But when I try to run this against SQL server, I get the following message:
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
Any suggestions how to accomplish the task of renumbering the keys?
The correct syntax doesn´t necessarily imply the use of a subquery:
update [TEST-PH].[dbo].[Emp_PosDepartments]
set Code = Code + 200
精彩评论