Можете да създадете потребителски дефиниран UDF, както е показано по-долу. След това просто предайте списъка, разделен със запетая, от друга заявка и тя ще върне таблица с всяка стойност в отделен ред.
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