sql 分离字符串获得数组的数据集合 支持两个字符串的分离 包括字符串数组的排序


--根据给定的数据源值,和数据源描述,返回数据集
--数据源值和数据源描述是以一定方式组合的单一字符串
CREATE FUNCTION [dbo].[GetArrayDataSet](@sourceVal nvarchar(4000),@sourceValDescription varchar(8000),@spliter varchar(10))
returns @temp table(elementVal varchar(8000),elementDescription nvarchar(4000),orderNum int)
as
begin
    declare @i int
    declare @j int
    declare @length int
   
    set @length = 0
    set @sourceVal = rtrim(ltrim(@sourceVal))
    set @sourceValDescription =  rtrim(ltrim(@sourceValDescription))
    set @i = charindex(@spliter,@sourceVal)
    set @j = charindex(@spliter,@sourceValDescription)

    while @i >= 1 and @j >= 1
    begin
        set @length = @length + 1
        insert @temp(elementVal,elementDescription,orderNum)
        values(
                    left(@sourceVal,@i - 1),
                    left(@sourceValDescription,@j - 1),
                    @length
                )
        set @sourceVal = substring(@sourceVal,@i + 1,len(@sourceVal) - @i)
        set @sourceValDescription = substring(@sourceValDescription,@j + 1,len(@sourceValDescription) - @j)
        set @i = charindex(@spliter,@sourceVal)
        set @j = charindex(@spliter,@sourceValDescription)
    end

    if @sourceVal <> '' and @sourceValDescription <> ''
    begin
        set @length = @length + 1
        insert @temp values(@sourceVal,@sourceValDescription,@length)
    end
    return
end
GO

select * from GetArrayDataSet('受理$审核$审批','stateInit$stateCheck$stateBatch','$')
--结果集(以表的方式返回):
--elementVal    elementDescription  orderNum
--受理            stateInit           1
--审核            stateCheck          2
--审批            stateBatch          3

你可能感兴趣的:(sql,J#,Go)