MSSQL 表值函数
时间:2010-09-13 来源:南阳源子
表值函数返回值是一个表。
CREATE FUNCTION [dbo].[Split] (@str nvarchar(max),@spliter nvarchar(10))
returns @tb table(Tmp nvarchar(256))
AS
BEGIN
DECLARE @Num int,@Pos int, @NextPos int
SET @Num = 0
SET @Pos = 1
WHILE(@Pos <= LEN(@str))
BEGIN
SELECT @NextPos = CHARINDEX(@spliter, @str, @Pos)
IF (@NextPos = 0 OR @NextPos IS NULL)
SELECT @NextPos = LEN(@str) + 1
INSERT INTO @tb VALUES(RTRIM(LTRIM(SUBSTRING(@str, @Pos, @NextPos - @Pos))))
SELECT @Pos = @NextPos+1
END
RETURN
END
使用方法:
select * from Split('1,2,3,4,5,6,7,8,9,0',',')
相关阅读 更多 +