-----------------------------------------------------------------------
-- 数据库 数组
-----------------------------------------------------------------------
create function myArray(@array nVARCHAR(max),@arrayStr VARCHAR(10))
returns @table table(m1 nvarchar(50))
AS
BEGIN
IF(RIGHT(@array,1) <> @arrayStr) --保证最右边是逗号
SET @array = @array + @arrayStr
declare @start INT --每次开始截取的位置
DECLARE @temp INT --每次最终截取的位置
declare @totalLength INT --传入的字符串的总长度
SET @totalLength = len(@array)
SET @start = 0;
SET @temp = 0;
WHILE(@start < @totalLength) --如果没有到最后
BEGIN
IF(substring(@array, @temp, 1) = @arrayStr) --探测下一个字符是不是逗号
BEGIN
insert into @table select substring(@array, @start, @temp-@start) --如果是,就从上一次逗号截取到这一次逗号
SET @start = @temp+1 --更新起始点,跳过逗号
END
SET @temp = @temp + 1 --向前进一位
END
return
END
declare @myStr nvarchar(1000)
set @mystr=N'1121 [姓名1]/1122 [姓名2]'
select * from myArray(@mystr,'/')