除了用 STRING_SPLIT重新排序 现在学习下面的函数,但是老数据没有更新,但不知道错误出现在哪里?特此提问请教
/***
-- 建函数 需要对应 数据库
create function fn_newnotext
(@notext nvarchar(29)) returns nvarchar(40)
as
begin
declare @n nvarchar(29)
declare @t table(x nvarchar(10))
insert into @t(x)
select substring(a.s,b.number,charindex(' ',a.s+' ',b.number)-b.number)
from (select @notext 's') a
inner join master.dbo.spt_values b
on b.[type]='P' and b.number between 1 and len(a.s) and substring(' '+a.s,b.number,1)=' '
order by substring(a.s,b.number,charindex(' ',a.s+' ',b.number)-b.number)
select @n=isnull(@n+' ','')+x
from @t
return @n
end
***/
----->更新前的老数据报表 每一行的数字不是从小到大的顺序
DROP TABLE IF EXISTS #test -- select * from #test
create table #test([MID] [int] IDENTITY(1,1) NOT NULL primary key,notext varchar(30))
insert into #test
select '30 51 54 09 16 77 67 59 13 43'
union all select '54 09 16 77 67 59 13 43 15 44'
union all select '16 77 67 59 13 43 15 44 63 52'
union all select '67 59 13 43 15 44 63 52 48 27'
union all select '13 43 15 44 63 52 48 27 11 03'
union all select '15 44 63 52 48 27 11 03 60 23'
union all select '63 52 48 27 11 03 60 23 12 75'
-- 更新
update [#test] set notext=dbo.fn_newnotext(notext)
-- 更新后数据 每行数字从小到大的顺序
select * from #test
order by MID
--想要的结果如下(上面的脚本执行后数据并没有改变)
---------------------------------------------
NoText
09 13 16 30 43 51 54 59 67 77
09 13 15 16 43 44 54 59 67 77
13 15 16 43 44 52 59 63 67 77
13 15 27 43 44 48 52 59 63 67
03 11 13 15 27 43 44 48 52 63
03 11 15 23 27 44 48 52 60 63
03 11 12 23 27 48 52 60 63 75