我在 SQL Server 2008 数据库中有一个表。我需要更新特定列的值但同时增加它的值。解释一下:
如果表是:
CREATE TABLE [dbo].[Player]
(
[PlayerID] [uniqueidentifier] NOT NULL,
[UnitID] [uniqueidentifier] NULL,
[ExerciseID] [uniqueidentifier] NOT NULL,
[Designation] [nvarchar](256) NOT NULL
)
我想遍历 ExerciseID
为 42C45D73-3FE6-4AFA-8E2F-09BDFC6CBDF7
的每一行,并将 Designation
更新为是 Player - X
但是 X 应该从 1 开始并且每次都自增 1。
因此,第一个要更新的玩家将是 Player - 1
,第二个将是 Player - 2
,依此类推。
我不知道从哪里开始做这样的事情!
谢谢
最佳答案
; with numbering as (
select PlayerID,
UnitID,
ExerciseID,
Designation,
row_number () over (order by PlayerID) - 1 rn
from Player
where ExerciseID = '42C45D73-3FE6-4AFA-8E2F-09BDFC6CBDF7'
)
update numbering
set Designation = 'Player - ' + convert(varchar(10), rn)
关于sql - 如何更新表中的所有行并增加一列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11742649/