sql 吧一个字符串逗号分隔转换成行
//调用fn_split1
select Id,Title
from ListTransField where Id in(
select value from [fn_Split1]((select TransField_Id from ListCompany where Company='客户公司'),',')
)
create FUNCTION [dbo].[fn_Split1](
@String nvarchar (max),
@Delimiter nvarchar (10)
)
RETURNS @ValueTable TABLE ([Value] NVARCHAR(max),[id] int)
BEGIN
DECLARE @NextString nvarchar(max),
@Pos int,
@NextPos int,
@CommaCheck nvarchar(1),
@id int
set @id=1
SET @NextString = ''
SET @CommaCheck = right(@String,1)
SET @String = @String + @Delimiter
SET @Pos = CHARINDEX(@Delimiter,@String)
SET @NextPos = 1
WHILE (@pos <> 0)
BEGIN
SET @NextString = SUBSTRING(@String,1,@Pos - 1)
INSERT INTO @ValueTable ( [Value],[id]) VALUES (@NextString,@id)
SET @String = SUBSTRING(@String,@pos +1,LEN(@String))
SET @NextPos = @Pos
SET @pos = CHARINDEX(@Delimiter,@String)
set @id = @id +1
END
RETURN
END
GO