USE [tms]
GO
/****** Object: UserDefinedFunction [dbo].[fn_ConvertListToTable_Sort] Script Date: 2017/4/26 9:04:51 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[fn_ConvertListToTable_Sort]
(
@String NVARCHAR(max), --要转换的字符串。
@Separator VARCHAR(10) = ‘,‘ --分隔符。
)
RETURNS @Table TABLE([ID] NVARCHAR(500),Sort int)
AS
BEGIN
SET @String = @String [email protected]
declare @int int=1
WHILE CHARINDEX(@Separator, @String) > 0
BEGIN
declare @str nvarchar(500)=‘‘
set @str=SUBSTRING(@String, 1, CHARINDEX(@Separator, @String) - 1)
INSERT INTO @Table ([ID],Sort) VALUES(@str,@int)
set @[email protected]+1
SELECT @String = SUBSTRING(@String, CHARINDEX(@Separator, @String) + 1, LEN(@String) - CHARINDEX(@Separator, @String))
END
return
END