使用子查询重新编号表中的键

时间:2022-08-16 00:12:57

I have a table that contains a list of deparments. The key ('code' in the sample) is just an assigned 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:

我有一个包含deparments列表的表。密钥(样本中的“代码”)只是一个分配的编号。不幸的是,这个表现在需要容纳一些带有预定义代码的新软件。当然,这些定义的代码与现有代码冲突。我想获取现有的键并将每个键增加200以将它们移出碰撞空间。我试过这个查询:

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:

但是当我尝试对SQL服务器运行时,我收到以下消息:

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?

有关如何完成重新编号键任务的任何建议吗?

1 个解决方案

#1


2  

The correct syntax doesn´t necessarily imply the use of a subquery:

正确的语法不一定意味着使用子查询:

update  [TEST-PH].[dbo].[Emp_PosDepartments]
set     Code = Code + 200

#1


2  

The correct syntax doesn´t necessarily imply the use of a subquery:

正确的语法不一定意味着使用子查询:

update  [TEST-PH].[dbo].[Emp_PosDepartments]
set     Code = Code + 200