USE [tms]
GO
/****** Object: UserDefinedFunction [dbo].[fn_StrToTable_Double] Script Date: 2017/4/26 9:07:38 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: HXQ
-- Create date: 2016年8月9日18:04:11
-- Description: 表值函数-将一个传入的字符串用2中分隔符拆分成临时表,例:SELECT * FROM dbo.fn_StrToTable_Double(‘111|222,333|444,555|666‘,‘,‘,‘|‘)
-- =============================================
ALTER FUNCTION [dbo].[fn_StrToTable_Double]
(
@String NVARCHAR(max), --要转换的字符串。
@Separator1 VARCHAR(10) = ‘,‘, --分隔符。
@Separator2 varchar(10)=‘|‘ --第二个分隔符
)
RETURNS @TableDouble TABLE([ID] NVARCHAR(500),[Text] NVARCHAR(500),Sort int)
AS
BEGIN
SET @String = @String [email protected]
declare @int int=1
WHILE CHARINDEX(@Separator1, @String) > 0
BEGIN
declare @str nvarchar(500)=‘‘,@str1 NVARCHAR(500)=‘‘
set @str=SUBSTRING(@String, 1, CHARINDEX(@Separator1, @String) - 1)
SET @str1=SUBSTRING(@str, 1, CHARINDEX(@Separator2, @str) - 1)
set @str=SUBSTRING(@str, CHARINDEX(@Separator2,@str)+1 , CHARINDEX(@Separator2,@str) - 1)
INSERT INTO @TableDouble ([ID],[Text],Sort) VALUES(@str1,@str,@int)
set @[email protected]+1
SELECT @String = SUBSTRING(@String, CHARINDEX(@Separator1, @String) + 1, LEN(@String) - CHARINDEX(@Separator1, @String))
END
return
END