Udostępnij za pośrednictwem


Lesson 2: Adding Parameters to Filter by Date

New: 17 July 2006

By including parameters for a start and end date in a query for your report, you can specify a date range that limits the data retrieved from the data source. You can create additional parameters to filter the data after it is retrieved from the data source.

In this lesson, you will add the query parameters @StartDate and @EndDate. You will see that case-sensitive report parameters StartDate and EndDate (without the query parameter indicator @) are automatically created for you. Note that different data providers use different query parameter indicators; for example, the Oracle data provider uses a colon (:) rather than the at (@) symbol.

You will set the data type of the parameters to DateTime and see that a calendar control appears with the parameter text box on the toolbar. You will set default values for the parameters so that the report can run automatically. Finally, you will create a report parameter DayofWeek that is not bound to a query parameter and use it to filter data after the data is retrieved from the data source.

To add date parameters to your query

  1. In Data view, select the AdventureWorks dataset.

  2. Replace the existing query with the following query:

    SELECT S.OrderDate, S.SalesOrderNumber, S.TotalDue, C.FirstName, 
        C.LastName
    FROM  HumanResources.Employee E INNER JOIN
          Person.Contact C ON E.ContactID = C.ContactID INNER JOIN
          Sales.SalesOrderHeader S ON E.EmployeeID = S.SalesPersonID
    WHERE (S.OrderDate BETWEEN (@StartDate) AND (@EndDate))
    -- Accurate Enddate would be '20020228 23:59:59.997'
    

    Note

    SQL Server stores DATETIME data accurate to one three-hundredth of a second. For more information, see Date and Time (Transact-SQL). If you do not receive the filtered results that you expected using a DateTime variable, you might need to use a more accurate time in your query parameter.

    In this tutorial, you will use approximate datetime values.

  3. Click Run (!) on the toolbar. The Define Query Parameters dialog box opens.

  4. In the Parameter Value column, enter a value for @StartDate. For example, 20010101.

  5. In the Parameter Value column, enter a value for @EndDate. For example, 20030101.

  6. Click OK. The report parameters StartDate and EndDate are automatically created and default to data type String. In the next three steps, you will set these parameters to data type DateTime.

  7. From the Report menu, click Report Parameters. The Report Parameters dialog box opens.

  8. In the Parameters list box, verify StartDate is highlighted. From the Data type drop-down list, select DateTime.

  9. In the Parameters list box, select EndDate. From the Data type drop-down list, select DateTime.

  10. Click OK.

  11. Click Preview. The StartDate and EndDate parameters each appear in the toolbar with a calendar control. Calendar controls automatically appear when the parameter is data type DateTime and you have not defined an available values list. Defining an available values list creates a drop-down list of values instead of a calendar control.

    In this tutorial, you will enter a value for the data instead of using the calendar control.

  12. In the StartDate parameter text box, enter the date 2001-01-01.

  13. In the EndDate parameter text box, enter the date 2003-01-01.

  14. Click View Report. The report displays only those data records that fall within the report parameter values.

After you have created query parameters for the report, you can add default values for those parameters. Default parameters allow the report to run automatically; otherwise, a user must enter parameter values for the report to run.

To set default values for date parameters

  1. In Layout view, from the Report menu, click Report Parameters. The Report Parameters dialog box opens.

  2. In the Parameters list box, verify StartDate is highlighted.

  3. In the default values section, select Non-queried and type 2001-01-01. Press the TAB key to exit the text box.

  4. In the Parameters list box, select EndDate.

  5. In the default values section, select Non-queried and type 2003-01-01. Press the TAB key to exit the textbox.

  6. Click OK.

  7. Click Preview. The report runs immediately because there are default values defined for all parameters.

    When report users run the report, they can set the defaults with parameter values that fall within the scope of the query parameters. For example, you can provide report parameter values that limit the rendered data to one month.

To add a new field to the query to use for filtering

  1. In Data view, select the AdventureWorks dataset.

  2. In the query, define an additional calculated column for the day of the week a sale occurred by adding the following command to the SELECT statement: DATENAME(weekday, S.OrderDate) as Weekday.

    Replace the existing query with the following text:

    SELECT S.OrderDate, DATENAME(weekday, S.OrderDate) as Weekday,
          S.SalesOrderNumber, S.TotalDue, C.FirstName, C.LastName
    FROM  HumanResources.Employee E INNER JOIN
          Person.Contact C ON E.ContactID = C.ContactID INNER JOIN
          Sales.SalesOrderHeader S ON E.EmployeeID = S.SalesPersonID
    WHERE (S.OrderDate BETWEEN (@StartDate) AND (@EndDate))
    
  3. Click Run (!). You should see a new column labeled Weekday and the names of the days of the week.

  4. In the Datasets window, verify that Weekday is a field.

    Note

    If the Datasets window is not visible, from the View menu, select Datasets.

    You have now added an additional dataset field Weekday. When you manually create a report parameter and bind it to this field, users can filter report data by day of the week.

To add a new a report parameter

  1. Click the Layout tab.

  2. From the Report menu, choose Report Parameters. The Report Parameters dialog box opens.

  3. Click Add. A new parameter, Report_Parameter_2, appears with default values.

  4. In the Properties section, in the Name text box, replace Report_Parameter_2 with DayoftheWeek. Verify that the data type is String.

  5. In Prompt, type Filter on which day of the week:.

  6. Clear the Allow blank option.

  7. In the Available values section, click Non-queried. Leave the entries blank.

  8. In the Default values section, click Non-queried. In the text box, type Sunday.

  9. Click OK.

To set a table filter with a parameter expression

  1. In Layout view, select the table. The table appears with a grey outline.

  2. Right-click on the table outline and select Properties. The Table Properties dialog box opens.

  3. Click the Filters tab.

  4. In the Expression column, from the drop-down list, select =Fields!Weekday.Value.

  5. Verify that the Operator is the equal sign (=).

  6. Click in the Value text box, and from the drop-down list, select <Expression>. The Edit Expression dialog box opens.

  7. In the global variables pane, click Parameters. The current list of parameters appears in the opposite pane.

  8. Double-click DayoftheWeek. The parameter expression is added to the expression text box. The following expression now appears in the expression text box: =Parameters!DayoftheWeek.Value.

  9. Click OK.

  10. Click OK.

    The filter for the table is now set to compare the value in the field Weekday with the parameter value for DayoftheWeek. For example, when you enter the value Sunday in the report toolbar for DayoftheWeek, the report processor will process only those rows in the table where the value for the field Weekday is Sunday.

  11. Click Preview. Because all parameters have default values, the report automatically runs. The table shows only values that fall within the date range defined by StartDate and EndDate and that fall on a Sunday.

  12. In the next optional procedure, you will add formatting to the table header.

(Optional) To set background and font color for the table header row

  1. Click the Layout tab.

  2. Click in the table. The table handles appear.

  3. Click the table header row. From the layout toolbar, click the Background Color button. The Choose Color dialog box opens.

  4. From the Web tab, choose DarkRed.

  5. Click OK.

    Next, choose the font color.

  6. From the layout toolbar, click the Foreground Color button. The Choose Color dialog box opens.

  7. From the Web tab, choose White.

  8. Click OK.

  9. Click Preview. The table renders with the colors chosen for the table header row.

Next Steps

You have successfully defined query parameters and report parameters, set default values for the parameters, and bound a report parameter to a calculated field. In the next lesson, you will learn to create an available values, or valid values, list for a parameter. See Lesson 3: Adding a Single Value Parameter with an Available Values List.

See Also

Tasks

Lesson 1: Creating a Report Server Project for the Parameter Tutorial

Other Resources

ISO 8601 Format

Help and Information

Getting SQL Server 2005 Assistance