在用户输入的参数中传递多个字符串值

时间:2018-03-19 22:22:43

标签: sql-server reporting-services ssrs-2008

我创建了一个SSRS,并且无法通过多个字符串值传递用户输入的参数。

我在下面的示例中遇到第3个SELECT问题。 如何逃避单引号并在两个参数中传递多个值?

谢谢!

CREATE TABLE dbo.#Cars (   
   Car_id int NOT NULL, 
   Color varchar(10), 
   Brand varchar(10), 
   Code int, 
) 

INSERT INTO dbo.#Cars VALUES(1, 'RED',   'Nissan',     555);
INSERT INTO dbo.#Cars VALUES(2, 'RED',   'BMW',        555);
INSERT INTO dbo.#Cars VALUES(3, 'BLUE',  'Toyota',     555);
INSERT INTO dbo.#Cars VALUES(4, 'GREEN', 'Nissan',     555);
INSERT INTO dbo.#Cars VALUES(5, 'BLACK', 'Alfa Romeo', 555);

SELECT * FROM dbo.#Cars;
SELECT * FROM dbo.#Cars WHERE Color IN ('RED', 'BLUE');

DECLARE @PARAM_COLOR VARCHAR(MAX);
DECLARE @PARAM_BRAND VARCHAR(MAX);
SET @PARAM_COLOR =  '''RED'',''BLUE''';
SET @PARAM_BRAND = '''Nissan'',''BMW''';
SELECT * FROM dbo.#Cars WHERE Color IN (@PARAM_COLOR) AND Brand IN (@PARAM_BRAND);

DROP TABLE dbo.[#Cars]

1 个答案:

答案 0 :(得分:0)

您可以像这样使用table variable

DECLARE @PARAM_COLOR TABLE (COLOR VARCHAR(100));
DECLARE @PARAM_BRAND TABLE (BRAND VARCHAR(100));

INSERT INTO @PARAM_COLOR
VALUES ('RED')
    ,('BLUE')
    ,('GREEN')
    ,('BLACK');

INSERT INTO @PARAM_BRAND
VALUES ('Nissan')
    ,('BMW')
    ,('Toyota');

SELECT *
FROM dbo.#Cars
WHERE Color IN (
        SELECT COLOR
        FROM @PARAM_COLOR
        )
    AND Brand IN (
        SELECT BRAND
        FROM @PARAM_BRAND
        );

附注:CREATE TABLE之后,您的Code int在示例中有一个额外的逗号:

CREATE TABLE dbo.#Cars (   
   Car_id int NOT NULL, 
   Color varchar(10), 
   Brand varchar(10), 
   Code int 
)