sql 报告:向SSRS报告SS.sql添加参数

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql 报告:向SSRS报告SS.sql添加参数相关的知识,希望对你有一定的参考价值。

/*
How to create a report that includes parameters that allows the user to input values that affect what data is pulled. Apparently, parameters can be created in two different ways:

1. By right-clicking the Parameters folder in the left side-bar and selecting Add Parameter (apparently this doesn't let you create optional parameters that are ignored if a value isn't entered), or

2. By creating the parameter within the report query (apparently this lets you create an optional parameter that is ignored if a value isn't entered)

Apparently the function of parameters (allowing user input to filter the report) can also be done by right-clicking a Dataset, going to Dataset Properties, and adding a Filter. Filtering within the query might be faster since the database engine is faster than the reporting services engine that would be used by the Filter. Can you add multiple optional filters this way?
*/

/*
HOW TO CREATE A SIMPLE PARAMETER:

1. Right-click the Parameters folder in the left side-bar and select Add Parameter - select the data type you want, and select 'Allow blank value("")' and/or 'Allow null value' if you want to allow those, or leave those unchecked if you want the parameter to be required, or

2a. Create the parameter in the report query, using a WHERE statement like WHERE customerID = @test_parameter (@test_parameter is the parameter, and customerID is the column in the result set that is associated with the parameter), and then

2b. Right-click the newly created parameter in the left side-bar, go to Parameter Properties, and then set the datatype and select if you want to allow blanks or NULLs.

Using the second option will hard-code the filtering of the data into the query of the report (so you don't have to add it in as a Filter), which could be faster since the query uses the database engine and a Filter would use the slower reporting services engine.
*/

SELECT customerID from orders
WHERE customerID = @test_parameter

/*
HOW TO CREATE AN OPTIONAL PARAMETER THAT IS IGNORED IF A VALUE IS NOT ENTERED WHERE A BLANK VALUE (IE NOT A NULL BUT A BLANK) IS PASSED IF NO ENTRY IS MADE (THIS MEANS THE USER DOES NOT HAVE TO CLICK A NULL CHECKBOX IN ORDER TO NOT PASS A VALUE) - ALL ROWS ARE RETURNED (THE PARAMETER IS IGNORED) IF A VALUE IS NOT ENTERED (IE A BLANK VALUE IS SUBMITTED):

This method creates a required parameter, but passes a blank value if the user doesn't submit a value (ie the parameter is ignored if a value isnt selected). Apparently you must create a parameter within the query using a WHERE statement in order to create a parameter that accepts NULLs and that is ignored if a value is not entered. This requires creating the parameter in a query statement, and then editing the Parameter Properties in a second step in order to create and expression that ignores the parameter if the user does not enter a value.

1. Create the parameter in the report query, using a WHERE statement like WHERE (a.OrderID = @p_orderID OR @p_orderID IS NULL) (the first part identifies the column associated with the parameter, and the second part with the IS NULL passes the NULL value to the parameter, in essence selecting all values) and then add an AND statement for each additional parameter, such as AND (a.ProductID = @p_productID OR @p_productID IS NULL)

2. Right-click the Parameters folder in the left side-bar and select Parameter Properties for each parameter that was created - select 'text' as the data type, check 'Allow blank value' (in which case the report will ignore the parameter), and uncheck 'Allow null value' (in which case the report will ignore the parameter).

3. Right-click the main dataset and select Dataset Properties for each parameter that was created - enter an expression that will tell it to ignore the parameter if no value is provided

The result will be a series of parameters that allow the entry of a value, or pass a blank value if no value is entered, thereby ignoring the parameter and returning all rows.

Examples of how this would work if there are two parameters, @first_parameter and @second_parameter:

EXAMPLE 1
3 entered for @first_parameter, 5 entered for @second_parameter
- result is all rows that have 3 in the column associated with @first_parameter AND 5 in column associated with @second_parameter

EXAMPLE 2
3 entered for @first_parameter, nothing entered for @second_parameter
- result is all rows that have 3 in the column associated with @first_parameter (it ignores the parameter @second_parameter since nothing was entered)

EXAMPLE 3
nothing entered for @first_parameter, 18 entered for @second_parameter
- result is all rows that have 18 in the column associated with @second_parameter (it ignores the parameter @first_parameter since nothing was entered)

EXAMPLE 4
nothing entered for @first_parameter, nothing entered for @second_parameter
- it ignores both @first_parameter and @second_parameter, leaving you with a completely unfiltered result set
*/

-- *STEP 1 - create the parameter in the query for the main dataset

SELECT a.OrderID, b.CustomerID, b.LastName, b.FirstName, c.ProductID, c.Product 
FROM orders1 a
JOIN customers1 b 
  ON a.CustomerID = b.CustomerID
JOIN products1 c
  ON a.ProductID = c.ProductID
WHERE (a.OrderID = @p_orderID OR @p_orderID IS NULL) 
  AND (a.ProductID = @p_productID OR @p_productID IS NULL) 
  AND (b.CustomerID IN (@p_customerID2) OR @p_customerID2 IS NULL)

-- DETAIL of code block above!!!

-- Query that is the basis of the report. This query can then be filtered down depending on what the user enters for the parameters.
SELECT a.OrderID, b.CustomerID, b.LastName, b.FirstName, c.ProductID, c.Product 
FROM orders1 a
JOIN customers1 b 
  ON a.CustomerID = b.CustomerID
JOIN products1 c
  ON a.ProductID = c.ProductID

-- Creates parameter @p_orderID (properties will still need to be set in left side-bar Parameter Properties), associates it with column OrderID, the OR part tells it to ignore this parameter if no value is entered [i.e. choose all values for the column if a value isn't entered])
WHERE (a.OrderID = @p_orderID OR @p_orderID IS NULL) 

-- Creates parameter @p_productID (properties will still need to be set in left side-bar Parameter Properties), associates it with column ProductID, the OR part tells it to ignore this parameter if no value is entered [i.e. choose all values for the column if a value isn't entered]) - having an AND means the results must satisfy both parameters - OR could also be used, but that would mean the results have to only satisfy one parameter OR the other.)
  AND (a.ProductID = @p_productID OR @p_productID IS NULL) 
  
-- Creates parameter @p_customerID (properties will still need to be set in left side-bar Parameter Properties), associates it with column ProductID, the OR part tells it to ignore this parameter if no value is entered [i.e. choose all values for the column if a value isn't entered]) - having an AND means the results must satisfy both parameters  
  AND (b.CustomerID IN (@p_customerID) OR @p_customerID IS NULL)
  
-- *STEP 2 - edit Parameter Properties for the newly created parameter

/* 
1. Right click and edit Parameter Properties
2a. On Parameter Properties > General tab, select Datatype 'text'
  2b. Check 'Allow blank value'
  2c. Uncheck 'Allow null value' (the parameter will require a value, but a blank value will be accepted)
3. Right click and edit Parameter Properties
*/

-- *STEP 3 - edit Dataset Properties for the main dataset

/* 
1. Right click the main dataset and edit Dataset Properties
2. On Dataset Properties > Parameters tab, find the parameter to edit, and next to the Parameter Value field, select the f(x) button to manually enter this expression (the parameter value name in this example is p_OrderID):

=IIF(Parameters!p_OrderID.Value ="",Nothing,Parameters!p_OrderID.Value)

*/

-- *STEP 4 - run the report

/*
1. For the parameter, enter a value to filter the report, or do not enter a value to have it pass a blank value and have it ignore the parameter, thereby returning all rows.
*/
  
/*
HOW TO CREATE AN OPTIONAL PARAMETER THAT IS IGNORED IF A VALUE IS NOT ENTERED WHERE USER CAN CLICK A NULL CHECKBOX - ALL ROWS ARE RETURNED (THE PARAMETER IS IGNORED) IF THE NULL CHECKBOX IS CHECKED:

This method creates a parameter that either requires a value, or which can pass a NULL value if the user selects a NULL checkbox (ie the parameter is ignored if the NULL checkbox is checked). Apparently you must create a parameter within the query using a WHERE statement in order to create a parameter that accepts NULLs and that is ignored if a value is not entered. This requires creating the parameter in a query statement, and then editing the Parameter Properties in a second step in order to identify the data type and allow the parameter to accept NULL values.

1. Create the parameter in the report query, using a WHERE statement like WHERE (a.OrderID = @p_orderID OR @p_orderID IS NULL) (the first part identifies the column associated with the parameter, and the second part with the IS NULL passes the NULL value to the parameter, in essence selecting all values) and then add an AND statement for each additional parameter, such as AND (a.ProductID = @p_productID OR @p_productID IS NULL)

2. Right-click the Parameters folder in the left side-bar and select Parameter Properties for each parameter that was created - select the data type you want, and select 'Allow null value' to allow NULLs to be submitted (in which case the report will ignore the parameter).

The result will be a series of parameters that allow the entry of a value, or pass a NULL value if the NULL checkbox is checked, thereby ignoring the parameter and returning all rows.

Examples of how this would work if there are two parameters, @first_parameter and @second_parameter:

EXAMPLE 1
3 entered for @first_parameter, 5 entered for @second_parameter
- result is all rows that have 3 in the column associated with @first_parameter AND 5 in column associated with @second_parameter

EXAMPLE 2
3 entered for @first_parameter, NULL checkbox checked for @second_parameter
- result is all rows that have 3 in the column associated with @first_parameter (it ignores the parameter @second_parameter since nothing was entered)

EXAMPLE 3
NULL checkbox checked for @first_parameter, 18 entered for @second_parameter
- result is all rows that have 18 in the column associated with @second_parameter (it ignores the parameter @first_parameter since nothing was entered)

EXAMPLE 4
NULL checkbox checked for @first_parameter, NULL checkbox checked for @second_parameter
- it ignores both @first_parameter and @second_parameter, leaving you with a completely unfiltered result set
*/

-- *STEP 1 - create the parameter in the query for the main dataset

SELECT a.OrderID, b.CustomerID, b.LastName, b.FirstName, c.ProductID, c.Product 
FROM orders1 a
JOIN customers1 b 
  ON a.CustomerID = b.CustomerID
JOIN products1 c
  ON a.ProductID = c.ProductID
WHERE (a.OrderID = @p_orderID OR @p_orderID IS NULL) 
  AND (a.ProductID = @p_productID OR @p_productID IS NULL) 
  AND (b.CustomerID IN (@p_customerID2) OR @p_customerID2 IS NULL)

-- DETAIL of code block above!!!

-- Query that is the basis of the report. This query can then be filtered down depending on what the user enters for the parameters.
SELECT a.OrderID, b.CustomerID, b.LastName, b.FirstName, c.ProductID, c.Product 
FROM orders1 a
JOIN customers1 b 
  ON a.CustomerID = b.CustomerID
JOIN products1 c
  ON a.ProductID = c.ProductID

-- Creates parameter @p_orderID (properties will still need to be set in left side-bar Parameter Properties), associates it with column OrderID, the OR part tells it to ignore this parameter if no value is entered [i.e. choose all values for the column if a value isn't entered])
WHERE (a.OrderID = @p_orderID OR @p_orderID IS NULL) 

-- Creates parameter @p_productID (properties will still need to be set in left side-bar Parameter Properties), associates it with column ProductID, the OR part tells it to ignore this parameter if no value is entered [i.e. choose all values for the column if a value isn't entered]) - having an AND means the results must satisfy both parameters - OR could also be used, but that would mean the results have to only satisfy one parameter OR the other.)
  AND (a.ProductID = @p_productID OR @p_productID IS NULL) 
  
-- Creates parameter @p_customerID (properties will still need to be set in left side-bar Parameter Properties), associates it with column ProductID, the OR part tells it to ignore this parameter if no value is entered [i.e. choose all values for the column if a value isn't entered]) - having an AND means the results must satisfy both parameters  
  AND (b.CustomerID IN (@p_customerID) OR @p_customerID IS NULL)
  
-- *STEP 2 - edit Parameter Properties for the newly created parameter

/*
1. Right click and edit Parameter Properties
2a. On Parameter Properties > General tab, select Datatype you want to use
  2b. Uncheck 'Allow null value' (the parameter will require a value, but a blank value will be accepted)
*/

-- *STEP 3 - run the report

/*
1. For the parameter, enter a value to filter the report, or check the NULL checkbox to pass a NULL value and have it ignore the parameter, thereby returning all rows.
*/


/*
HOW TO CREATE A PARAMETER THAT GIVES USER A SINGLE-SELECT DROPDOWN OF DISTINCT CHOICES FOR SELECTING THE VALUE WHERE THE PARAMETER IS IGNORED IF NO VALUE IS ENTERED:

Rather than typing in the value, you can instead set it up so the user has a dropdown of choices to choose from. This is a good way to ensure that the user select legit values for the parameter. Apparently this makes the parameter required (you can't leave it blank). There are three steps:

1. Create your parameter (either through the query or by clicking Add Parameter in the left side-bar), and edit Parameter Properties to select data type and allow NULLs or multiple values if you want.

2. Right click Datasets and choose Add Dataset.  

3a. In the left side-bar, edit the Parameter Properties.

  3b. Click the Available Values tab.

  3c. Click 'Get values from a query'.

  3d. For Dataset, select the Dataset that you created from a query in step 2.

  3e. For Value Field, select the column that you want to use for values.

  3f. For Label Field, select the same column name you selected in step 3e.
*/

-- Query used to create the parameters (step 1)

SELECT a.OrderID, b.CustomerID, b.LastName, b.FirstName, c.ProductID, c.Product 
FROM orders1 a
JOIN customers1 b 
  ON a.CustomerID = b.CustomerID
JOIN products1 c
  ON a.ProductID = c.ProductID
WHERE (a.OrderID = @p_orderID OR @p_orderID IS NULL)
  AND (a.ProductID = @p_productID OR @p_productID IS NULL)
  AND (b.CustomerID IN (@p_customerID) OR @p_customerID IS NULL)
  
-- Query used to pull the values available in the dropdown (step 2)

SELECT DISTINCT OrderID FROM orders1
UNION
SELECT NULL 
ORDER BY CustomerID ASC

-- DETAIL of query used to pull values!!!

SELECT DISTINCT OrderID FROM orders1  -- selects all distinct values from column OrderID from orders1 table
UNION                                 -- joins the results from the part below with the part above
SELECT NULL                           -- adds NULL value to results set (the parameter must be able to accept NULLs)
ORDER BY CustomerID ASC               -- sorts by ascending, so that NULL is the top value

/*
HOW TO CREATE A PARAMETER THAT GIVES USER A MUTLI-SELECT DROPDOWN OF DISTINCT CHOICES FOR SELECTING THE VALUE WHERE THE PARAMETER IS IGNORED IF NO VALUE IS PROVIDED [IE, SINCE A MULTI-SELECT CANNOT HAVE A NULL VALUE, YOU INSTEAD HAVE TO PUT 'select all' AS THE DEFAULT VALUE FOR THE MULT-SELECT DROPDOWN]:

Rather than typing in the value, you can instead set it up so the user has a dropdown of choices to choose from. This is a good way to ensure that the user select legit values for the parameter. Apparently this makes the parameter required (you can't leave it blank). There are three steps:

1. Create your parameter (either through the query or by clicking Add Parameter in the left side-bar), and edit Parameter Properties to select data type and allow NULLs or multiple values if you want.

2. Right click Datasets and choose Add Dataset.  

3a. In the left side-bar, edit the Parameter Properties.

  3b. Click the Available Values tab.

  3c. Click 'Get values from a query'.

  3d. For Dataset, select the Dataset that you created from a query in step 2.

  3e. For Value Field, select the column that you want to use for values.

  3f. For Label Field, select the same column name you selected in step 3e.

  3g. In Parameter Properties, click Default Values

  3h. For Dataset, select the Dataset that you created from a query in step 2 (same Dataset as you selected in 3d).

  3i. For Value Field, select the column that you want to use for values.
*/

-- Query used to create the parameters (step 1)

SELECT a.OrderID, b.CustomerID, b.LastName, b.FirstName, c.ProductID, c.Product 
FROM orders1 a
JOIN customers1 b 
  ON a.CustomerID = b.CustomerID
JOIN products1 c
  ON a.ProductID = c.ProductID
WHERE (a.OrderID = @p_orderID OR @p_orderID IS NULL)
  AND (a.ProductID = @p_productID OR @p_productID IS NULL)
  AND (b.CustomerID IN (@p_customerID) OR @p_customerID IS NULL)
  
-- Query used to pull the values available in the dropdown (step 2)

SELECT DISTINCT OrderID FROM orders1


-- DETAIL of query used to pull values!!!

SELECT DISTINCT OrderID FROM orders1  -- selects all distinct values from column OrderID from orders1 table. Not that for 'select all' to be the default value for the multi-select parameter, the result set from this query cannot contain a NULL value (this is different that the query for the single-select dropdown, where you want to add a NULL value so that NULL can be passed along if a selection is not made).

以上是关于sql 报告:向SSRS报告SS.sql添加参数的主要内容,如果未能解决你的问题,请参考以下文章

where子句中的SSRS参数用法

使用查询作为输入参数创建动态SSRS报告

sql 手动执行SSRS报告

用于测试的硬编码SSRS多值参数

如何使用报告向导修改现有SSRS报告

通过SSRS SQL Server 2012更新的正常运行时间/停机时间生产批处理报告