我有一个非常有趣的问题。我有一个带有多项选择下拉菜单的 SSRS 报告。下拉菜单允许选择多个值或所有值。所有的价值观都不是问题。
问题是 1 或多个选项的组合
当我在下拉菜单中选择“AAA”时,它应该返回 3 个值:“AAA”、“AAA 1”、“AAA 2”
现在只返回 1 个值。
问题:
如何使 IN 语句像 LIKE 一样工作?
The Drop down select
SELECT '(All)' AS team, '(All)' AS Descr
UNION ALL
SELECT 'AAA' , 'AAA'
UNION ALL
SELECT 'BBB' , 'BBB'
Table Mytable
ColumnA Varchar(5)
Values for ColumnA
'AAA'
'AAA 1'
'AAA 2'
'BBB'
'BBB 1'
'BBB 2'
SELECT * FROM Mytable
WHERE ColumnA IN (SELECT * FROM SplitListString(@Team, ',')))
Split function
CREATE FUNCTION [dbo].[SplitListString]
(@InputString NVARCHAR(max), @SplitChar CHAR(1))
RETURNS @ValuesList TABLE
(
param NVARCHAR(MAX)
)
AS
BEGIN
DECLARE @ListValue NVARCHAR(max)
DECLARE @TmpString NVARCHAR(max)
DECLARE @PosSeparator INT
DECLARE @EndValues BIT
SET @TmpString = LTRIM(RTRIM(@InputString));
SET @EndValues = 0
WHILE (@EndValues = 0) BEGIN
SET @PosSeparator = CHARINDEX(@SplitChar, @TmpString)
IF (@PosSeparator) > 1 BEGIN
SELECT @ListValue = LTRIM(RTRIM(SUBSTRING(@TmpString, 1, @PosSeparator -1 )))
END
ELSE BEGIN
SELECT @ListValue = LTRIM(RTRIM(@TmpString))
SET @EndValues = 1
END
IF LEN(@ListValue) > 0 BEGIN
INSERT INTO @ValuesList
SELECT @ListValue
END
SET @TmpString = LTRIM(RTRIM(SUBSTRING(@TmpString, @PosSeparator + 1, LEN(@TmpString) - @PosSeparator)))
END
RETURN
END