Du kan skapa en användardefinierad UDF som den som visas nedan. Skicka sedan bara in den kommaseparerade listan från en annan fråga och den returnerar en tabell med varje värde i en separat rad.
CREATE FUNCTION [dbo].[fnSplitStringAsTable]
(
@inputString varchar(MAX),
@delimiter char(1) = ','
)
RETURNS
@Result TABLE
(
Value varchar(MAX)
)
AS
BEGIN
DECLARE @chIndex int
DECLARE @item varchar(100)
-- While there are more delimiters...
WHILE CHARINDEX(@delimiter, @inputString, 0) <> 0
BEGIN
-- Get the index of the first delimiter.
SET @chIndex = CHARINDEX(@delimiter, @inputString, 0)
-- Get all of the characters prior to the delimiter and insert the string into the table.
SELECT @item = SUBSTRING(@inputString, 1, @chIndex - 1)
IF LEN(@item) > 0
BEGIN
INSERT INTO @Result(Value)
VALUES (@item)
END
-- Get the remainder of the string.
SELECT @inputString = SUBSTRING(@inputString, @chIndex + 1, LEN(@inputString))
END
-- If there are still characters remaining in the string, insert them into the table.
IF LEN(@inputString) > 0
BEGIN
INSERT INTO @Result(Value)
VALUES (@inputString)
END
RETURN
END