34,590
社区成员
发帖
与我相关
我的任务
分享
alter function dbo.resort(@x varchar(8000))
returns varchar(8000)
as
begin
declare @y varchar(8000);
with t as(
select val=o.value('.','int')
from (select S=convert(xml,'<r><n>'+replace(@x,',','</n><n>')+'</n></r>')) t
cross apply S.nodes('/r/n') x(o))
select @y=isnull(@y+',','')+rtrim(val)
from t
order by val
return @y
end
create table #t(x varchar(100))
insert into #t(x) select '3,1,2,7,8'
create function dbo.resort(@x varchar(100))
returns varchar(100)
as
begin
declare @y varchar(100);
with t as(
select val=o.value('.','int')
from (select S=convert(xml,'<r><n>'+replace(@x,',','</n><n>')+'</n></r>')) t
cross apply S.nodes('/r/n') x(o))
select @y=isnull(@y+',','')+rtrim(val)
from t
order by val
return @y
end
update t
set t.x=dbo.resort(x)
from #t t
select * from #t
/*
x
--------------------------
1,2,3,7,8
(1 行受影响)
*/