日期:2014-05-17 浏览次数:20431 次
create table dbo.ded(sname varchar(20))
insert into dbo.ded(sname)
select '李维一' union all
select '张嘉凯' union all
select '王小刚' union all
select '采薇'
with t as
( select row_number() over(order by getdate()) 'rn',
sname
from dbo.ded
)
update a
set a.sname=case when a.rn%2=0 then (select left(sname,1) from t where rn=a.rn-1)
else (select left(sname,1) from t where rn=a.rn+1) end
+substring(a.sname,2,10)
from t a
select * from dbo.ded
/*
sname
--------------------
张维一
李嘉凯
采小刚
王薇
(4 row(s) affected)
*/
避免逻辑复杂,我使用了临时表,把数据在临时表做好。再修改到实际表内
Create table #s (sname varchar(20))
insert into #s select '李维一'
union all select '张嘉凯'
union all select '王小刚'
union all select '采薇'
Create table #t (sname varchar(20))
insert into #t select '李维一'
union all select '张嘉凯'
union all select '王小刚'
union all select '采薇'
with t1 as (select sname,px=row_number()over(order by getdate()) from #s ),
t2 as (select sname,px=row_number()over(order by getdate()) from #t )
select * into #a from (
select name1,name2
from
(select sname as name1,px from t1 where px%2=1)a
join
(select sname as name2,px from t2 where px%2=0)b on a.px=b.px-1
union all
select name1,name2
from
(select sname as name1,px from t1 where px%2=0)a
join
(select sname as name2,px from t2 where px%2=1)b on a.px=b.px+1
)ab
--select * from #a
update #a set name1=left(ltrim(name1),1)+substring(ltrim(name2),2,len(ltrim(name2)))
select name1 from #a
USE test
GO
-->生成表tb
if object_id('tb') is not null