我是存储过程的新手。
我有400万条记录,因此手动无法使用存储过程。
我有一张表:
Id Name
-----------------
1 abc
2 xyz
3 abc
4 pqr
5 abc
6 pqr
在该表中,一个名为Name。在Name列中,有些记录是同名的,所以我想修改记录并希望:
Id Name
---------------------
1 abc
2 xyz
3 abc-1
4 pqr
5 abc-2
6 pqr-1
&安培;将其插入到具有相同模式的另一个表中。
答案 0 :(得分:0)
您可以使用可更新的CTE执行此操作:
with toupdate as (
select t.*, row_number() over (partition by name order by id) as seqnum
from onetable t
)
update toupdate
set name = name + '-' + cast(seqnum - 1 as varchar(255))
where seqnum > 1;
实际上,它会更新它。把它放到另一张表中:
with toinsert as (
select t.*, row_number() over (partition by name order by id) as seqnum
from onetable t
)
select id,
(case when seqnum = 1 then name
else name + '-' + cast(seqnum - 1 as varchar(255))
end) as name
into newtable
from toinsert;
答案 1 :(得分:0)
这将更新表格
;WITH cte AS
(
SELECT id,
ROW_NUMBER() OVER(PARTITION BY Name ORDER BY Id) AS rno,
FROM table1
)
update t.Name = t.Name + '-'+ c.rno
from table1 t
join cte c on c.id = t.id
where c.rno >1
要插入,只需使用charindex
select * into Table2 from table1
where CHARINDEX('-',name) > 1