看到一个sql server 的比较有用的function 可以方便的把传入的字符串输出成表格,希望能跟大家分享
CREATE FUNCTION dbo.GetTableVarchar(@idList varchar(max))
RETURNS
@ParsedList table
(
id int
)
AS
BEGIN
DECLARE @id varchar(10), @Pos int
SET @idList = LTRIM(RTRIM(@idList))+ ','
SET @Pos = CHARINDEX(',', @idList, 1)
IF REPLACE(@idList, ',', '') <> ''
BEGIN
WHILE @Pos > 0
BEGIN
SET @id = LTRIM(RTRIM(LEFT(@idList, @Pos - 1)))
IF @id <> ''
BEGIN
INSERT INTO @ParsedList (id)
VALUES (CAST(@id AS int)) --Use Appropriate conversion
END
SET @idList = RIGHT(@idList, LEN(@idList) - @Pos)
SET @Pos = CHARINDEX(',', @idList, 1)
END
END
RETURN
END
当然你可以根据需要更改输出和输入的数据类型:)RETURNS
@ParsedList table
(
id int
)
AS
BEGIN
DECLARE @id varchar(10), @Pos int
SET @idList = LTRIM(RTRIM(@idList))+ ','
SET @Pos = CHARINDEX(',', @idList, 1)
IF REPLACE(@idList, ',', '') <> ''
BEGIN
WHILE @Pos > 0
BEGIN
SET @id = LTRIM(RTRIM(LEFT(@idList, @Pos - 1)))
IF @id <> ''
BEGIN
INSERT INTO @ParsedList (id)
VALUES (CAST(@id AS int)) --Use Appropriate conversion
END
SET @idList = RIGHT(@idList, LEN(@idList) - @Pos)
SET @Pos = CHARINDEX(',', @idList, 1)
END
END
RETURN
END