以下语句将返回 CSV 参数类别中指定的所有产品:
DECLARE @Products TABLE
(
[ProductID] INT
,[ProductName] NVARCHAR(32)
)
DECLARE @Categories TABLE
(
[CategoryID] INT
,[CategotyName] NVARCHAR(32)
)
DECLARE @ProductCategory TABLE
(
[ProductID] INT
,[CategoryID] INT
)
INSERT INTO @Products ( [ProductID], [ProductName])
VALUES (1, 'Product 1')
,(2, 'Product 2')
,(3, 'Product 3')
,(4, 'Product 4')
INSERT INTO @Categories ( [CategoryID], [CategotyName])
VALUES (1, 'Category 1')
,(2, 'Category 2')
,(3, 'Category 3')
INSERT INTO @ProductCategory ( [ProductID], [CategoryID])
VALUES (1, 1)
,(1, 2)
,(1, 3)
,(2, 1)
,(2, 2)
,(3, 1)
,(4, 2)
,(4, 3)
DECLARE @CategoriesCSV NVARCHAR(MAX) = '1,2'
DECLARE @CategoriesXML XML = N'<r><![CDATA[' + REPLACE(@CategoriesCSV, ',', ']]></r><r><![CDATA[') + ']]></r>'
;WITH DataSource AS
(
SELECT DISTINCT CAST(Tbl.Col.value('.', 'float') AS BIGINT) AS [CategoryID]
FROM @CategoriesXML.nodes('//r') Tbl(Col)
WHERE ISNUMERIC(Tbl.Col.value('.', 'varchar(max)')) = 1
)
SELECT C.[CategoryID]
,C.[CategotyName]
,P.[ProductID]
,P.[ProductName]
FROM @Categories C
INNER JOIN DataSource DS
ON C.[CategoryID] = DS.[CategoryID]
INNER JOIN @ProductCategory PC
ON C.[CategoryID] = PC.[CategoryID]
INNER JOIN @Products P
ON PC.[ProductID] = P.[ProductID]
上述查询的结果是: