日期:2014-05-17 浏览次数:20543 次
CREATE TABLE [dbo].[testx1]
(
[id] [bigint] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](50) NOT NULL
)
insert into testx1 VALUES('A')
insert into testx1 VALUES('A')
insert into testx1 VALUES('B')
insert into testx1 VALUES('C')
insert into testx1 VALUES('C')
insert into testx1 VALUES('C')
insert into testx1 VALUES('D')
insert into testx1 VALUES('A1')
insert into testx1 VALUES('D')
insert into testx1 VALUES('B')
create function fn_split(@name varchar(200))
returns varchar(200)
as
begin
declare @sub varchar(200)
declare @index int
set @index=1
set @sub=substring(@name,@index,1)
while 1=1
begin
if(@sub='0' or @sub='1' or @sub='2' or @sub='3' or @sub='4' or @sub='5' or @sub='6' or @sub='7' or @sub='8' or @sub='9')
break
set @index =@index + 1
if(@index-1=len(@name))
break
set @sub=substring(@name,@index,1)
end
return substring(@name,1,@index-1)
end
;with t as
(
select id,name,(select count(1) from [testx1] a where a.id<=b.id and dbo.fn_split(a.name)=dbo.fn_split(b.name)) number from [testx1] b
)
update t set name=dbo.fn_split(name)+convert(varchar(20),number-1) where number<>1