注册 登录
编程论坛 SQL Server论坛

请问怎么给重复名字加个编号

ly2838350 发布于 2021-03-18 18:51, 3789 次点击
比如有两个张三,两个李四,改成张三1,张三2,李四1,李四2
求教大佬们~~~急
3 回复
#2
oldfish962021-04-25 12:12
程序代码:
DECLARE @t TABLE( id INT, name VARCHAR(10))

INSERT @t
VALUES
( 1, '王五' ),
( 2, '王五' ),
( 3, '王五' ),
( 4, '李三' ),
( 5, '王五' ),
( 6, '赵四' ),
( 10, '田强' ),
( 15, '田强' ),
( 18, '李三' )
SELECT * FROM @t AS T


DECLARE @th TABLE( rank1 INT, id INT, name VARCHAR(10))
INSERT @th SELECT ROW_NUMBER() OVER (PARTITION BY name ORDER BY T.id ), T.id, T.name FROM @t AS T


UPDATE T SET T.name += CONVERT(VARCHAR(20), T2.rank1) FROM @t AS T JOIN @th AS T2 ON T2.id = T.id

SELECT * FROM @t AS T ORDER BY T.id


只有本站会员才能查看附件,请 登录


[此贴子已经被作者于2021-4-27 15:30编辑过]

#3
sssooosss2021-04-26 09:11
共同学习
#4
xianyou_2012022-02-17 17:55
从零学起很难吗?
1