The Report Authoring ProcessThe report authoring process includes creating a new Reporting Services project using SQL Server Business Intelligence Development Studio, defining a data sou
Trang 1TABLE 73-1
Visual Studio 2008 Report Items
Report Item Description
Textbox Adds report content that is not located within a table or matrix The textbox
can contain static text or an expression or can be bound to a data field
Line, Rectangle Adds visual separation and formatting to a report Rectangles can also be
used to group other items, enabling them to be treated as a group for placement and/or visibility They also enable multiple controls to be placed where, by default, you can only place a textbox
Image Places an image in the report The image source can be Embedded, Project,
Database, or the Web
Table Renders the dataset in a Tablix with a fixed set of columns The item
contains many options to control how the data is grouped, sorted, exported, and presented A report can contain multiple tables, providing the capability
to include data from multiple datasets and data sources in a single report
Begin with a Table instead of a Matrix if the data will be presented primarily as fixed columns
Matrix Renders a dataset as a crosstab For example, the Matrix report item could
show total sales by region as row headers, and periods as column headers, enabling the column headers to change over time based on the underlying dataset Multiple column, row, and detail criteria can be added to the Matrix report item Begin with a Matrix instead of a Table when the data will be presented primarily with variable column headers
List The list is bound to a dataset The content of a list is repeated for each row
in the dataset or for each grouped row if group criteria are specified The body of the list represents the template for the report items to be displayed
The report author places items within the template, free-form, without the spatial constraints of a table
Chart This item includes a wide variety of charts and provides extensive control
over the chart type and formatting
Gauge Similar to charts, gauges come in a wide variety of shapes and sizes But
unlike charts, which show many data points, gauges display a single data point For example, a gauge might be used to display actual sales vs a planned sales goal
Subreport Use this item to render another report within the current report, usually
displaying some details not available from the dataset driving the current report
Trang 2The Report Authoring Process
The report authoring process includes creating a new Reporting Services project using SQL Server
Business Intelligence Development Studio, defining a data source for the report, adding a report to the
project, creating a dataset to retrieve the report data, and formatting the report content This section
describes the primary tasks required to create a report and provides an example to illustrate the process
Creating a Reporting Services project
Business Intelligence Development Studio, a version of Visual Studio 2008 shipped with SQL Server,
contains the tools required to author and deploy Reporting Services reports Alternately, developers with
a license for Visual Studio can open a reporting project within that environment Follow these steps to
create a new Reporting Services project:
1 Run the Business Intelligence Development Studio (BIDS).
2 Select File➪ New ➪ Project to open the New Project dialog Alternately, you can click the
Create Project link in the Recent Projects section of the Start Page tab to open the New Project
dialog By default, the Start Page tab is displayed when opening BIDS
3 Select the Business Intelligence Projects project type in the Project Types pane on the left side.
4 Select the Report Server Project template in the Templates pane on the right side.
5 Name the project.
6 Specify the location of the project To create a folder to contain the Solution file with a
sub-folder containing the report project, check the option to Create Directory for Solution The
value provided for the Solution Name field specifies the name of the solution folder, while
the value provided for the Name field specifies the name of the report project To create the
solution and project under a single folder, uncheck the option Create Directory for Solution
7 Select OK to create the new Reporting Services project.
An alternative to the Report Server Project is the Report Server Project Wizard, which invokes the
Report Wizard to guide the creation of an initial report in addition to creating a project
Creating a report
Two methods are available to create a report: using the report wizard and adding a blank report The
following steps describe the tasks required to create a report, regardless of the method:
1 Add a report to the report project by selecting Project➪ Add New Item Select the Report
Wizard template to have the wizard help you create a report, or select the Report template to
create a blank report
2 Create a (or select an existing) data source.
3 Create one or more report datasets.
4 Design the report layout.
5 Add and configure Report Parameters.
6 Use the Preview tab on the Report Designer to preview the report.
Trang 3Using the Report Wizard to create reports
The Report Wizard is a good way to build a simple report quickly The Report Wizard will walk you
through selecting (or creating) the data source, designing the report query, choosing the report type,
adding data fields to the report, choosing the report style, and naming the report This takes as little as
60 seconds, and when completed you have a report that’s ready to run
If you could satisfy all of your report requirements using the wizard, that would be wonderful However,
this is typically just a starting point After creating a report using the wizard, the Report Designer can be
used to customize the report
Authoring a report from scratch
Starting with a blank report requires manual completion of all six steps outlined above, but it allows for
more flexibility The following example demonstrates how to build a simple product list report using the
AdventureWorks2008sample database
Creating a shared data source
Figure 73-2 shows the Shared Data Source Properties dialog configured for theAdventureWorks2008
database
FIGURE 73-2
Creating a shared data source
Trang 4Best Practice
While it is possible to create report data sources without referring to a shared data source, it is generally
best to avoid this practice When reports are deployed to different environments, or systems undergo
a configuration change, it is much easier to adjust a handful of shared data sources than every deployed
report A report-specific data source can be converted to a shared data source by right-clicking it in the
Report Data pane and choosing ‘‘Convert to Shared Data Source.’’
Follow these steps to create a new shared data source:
1 With the Solution Explorer displaying the reporting project, right-click on the Shared Data
Sources folder and choose Add New Data Source The Shared Data Source Properties dialog
will appear
2 Name the Data Source ‘‘AdventureWorks’’.
3 Select Microsoft SQL Server from the Type drop-down list.
4 Enter the connection string to theAdventureWorks2008database You can click the Edit
button to build the connection string
5 Switch to the Credentials tab and choose an appropriate login method for the data source.
6 Click OK to add the new data source to the project.
Adding a blank report
Follow these steps to create a blank report:
1 With the Solution Explorer displaying a reporting project, right-click on the Reports folder and
choose Add➪ New Item
2 From the Add New Item dialog, select the Report template.
3 Name the report ‘‘Project List’’.
4 Click Add.
The new empty report is added to your project and opened in the Report Designer The Report Designer
window contains two tabs: Design and Preview Supporting panes (usually located to the left of the
design surface) are the Toolbox that contains items to be placed on the design surface and the Report
Data pane, which shows the sources of data available to build up the report, as well as parameters and
images
Creating a dataset
Follow these steps to create a dataset to return product information:
1 Define a data source that refers to the shared data source created earlier In the Report Data
pane, choose New➪ Data Source In the Data Source Properties dialog, name the Data Source
‘‘AdventureWorks’’, select the ‘‘Use shared Data Source reference’’ option, and choose the
AdventureWorks shared data source from the drop-down
Trang 52 Right-click on the AdventureWorks data source in the Report Data pane and choose Add
Dataset The Dataset Properties dialog will appear (see Figure 73-3)
FIGURE 73-3
Use the Data Properties dialog to define the query that will populate the dataset
3 Name the dataset ‘‘Products’’.
4 Select the AdventureWorks data source.
5 Enter the following SQL (this SQL statement returns the product information, including the
Category and Subcategory names and IDs from theAdventureWorks2008database):
SELECT P.ProductID, P.[Name], P.ProductNumber, P.MakeFlag, P.Color, P.[Size], P.ListPrice, P.StandardCost,
P.ProductSubcategoryID, SubCat.[Name] AS Subcategory, SubCat.ProductCategoryID, Cat.[Name] AS Category FROM Production.Product P
INNER JOIN Production.ProductSubcategory SubCat
ON P.ProductSubcategoryID = SubCat.ProductSubcategoryID INNER JOIN Production.ProductCategory Cat
ON SubCat.ProductCategoryID = Cat.ProductCategoryID
6 Press the Refresh Fields button to verify the SQL, or press the Query Designer button to
pre-view the data Once the Dataset Properties dialog has been dismissed using the OK button, the new dataset and associated fields will appear under the data source in the Report Data pane
Trang 6Displaying data in the report
The results can be displayed in the report using either the list, table, or matrix report items Working
with these items is covered in more depth later in this chapter To display the results of the dataset,
fol-low these steps to add a table to the body of the report, bind it to the dataset, and add data fields to
the table:
1 Select the Design tab on the Report Designer.
2 Add a table to the body of the report by dragging a table report item from the Toolbox and
dropping it on the report body
3 Add fields from the dataset by dragging fields from the Report Data pane and dropping them
in the details row of the table
Previewing the report
Select the Preview tab to review the report The report currently consists of a single table that displays
data from the Products dataset, which in turn queries data from the AdventureWorks data source
Details on how to refine both the data and display of the report follow in the next sections
Working with Data
Once the basics of building up a report are understood, the next step is to dive into the details of
struc-turing data to meet reporting needs The Query Designer provides assistance in constructing queries for
different data sources, while parameters provide a way to limit and filter result sets returned by those
queries Expressions provide a way to combine results and parameters to synthesize data and behaviors
Best Practice
Offloading as much work to the source database as possible will increase the speed of report execution
Use query parameters to return only the data actually required for display When a report represents a
summary of source data, let the source database aggregate to the granularity of the report Reporting Services
can filter and summarize data as well, but it will never match a database engine’s speed
Working with SQL in the Query Designer
The Query Designer can be invoked from the Dataset Properties dialog by clicking the Query Designer
button, or by right-clicking on a dataset in the Report Data pane and choosing Query The default view,
Edit as Text, displays the query as text in the upper pane, and the query results in the lower pane Click
the exclamation point in the toolbar to execute the query
Many programmers find that developing queries in their favorite SQL tool and pasting the query text
into the Query Designer or Dataset Properties dialogs provides the most efficient workflow, but the
Query Designer also offers a graphical mode Click the Edit as Text button to toggle to the graphical
designer, which contains the Diagram, Grid, SQL, and Results panes Figure 73-4 shows the graphical
designer view containing the Products query used earlier in this chapter
Trang 7FIGURE 73-4
The Advanced Query Designer simplifies writing SQL command text
Using query parameters to select and filter data
Query parameters appear in a query much like a T-SQL variable would for SQL Server data sources For
example, the following query limits the rows returned to only those with a specific subcategory:
SELECT * FROM Production.Product WHERE ProductSubcategoryID = @ProductSubcategoryID
This example refers to a query parameter named@ProductSubcategoryID, which in turn is mapped
to either a report parameter or an expression The mapping is specified on the Parameters tab of the
Dataset Properties dialog However, if a query parameter is to be mapped to a report parameter of the
same name, simply refer to the query parameter, as shown, and the corresponding report parameter will
be created and mapped when the dataset is saved
Query parameters are specific to a particular dataset and are visible only in the Dataset Properties
dia-log Report parameters are global to the report, can be set to prompt the user when the report runs, and
appear in the Report Data pane under the Parameters folder
Trang 8Nested report parameters
This section offers an interesting example of how to prompt the user for report parameters and how
those parameters can interact The goal is to generate a report that lists products within a given product
category and subcategory For usability, the report user will be prompted for both the category and the
subcategory Of course, the subcategories change with each selection of a category — these are referred
to as nested parameters.
To create this nested parameter example, you’ll add two datasets to the Product List report created
ear-lier Both new datasets use the AdventureWorks data source The first dataset, namedCategoryList,
returns the list of categories using the following SQL:
SELECT ProductCategoryID, Name
FROM Production.ProductCategory
ORDER BY Name
The second dataset, namedSubcategoryList, returns the list of subcategories and includes a
param-eter to select only the subcategories for the selected category:
SELECT ProductSubcategoryID, ProductCategoryID, Name
FROM Production.ProductSubcategory
WHERE (ProductCategoryID = @ProductCategoryID)
ORDER BY Name
Update theProductsdataset with the following SQL to include the new parameters:
SELECT P.ProductID, P.[Name], P.ProductNumber, P.MakeFlag,
P.Color, P.[Size], P.ListPrice, P.StandardCost,
P.ProductSubcategoryID, SubCat.[Name] AS Subcategory,
SubCat.ProductCategoryID, Cat.[Name] AS Category
FROM Production.Product P
INNER JOIN Production.ProductSubcategory SubCat
ON P.ProductSubcategoryID = SubCat.ProductSubcategoryID
INNER JOIN Production.ProductCategory Cat
ON SubCat.ProductCategoryID = Cat.ProductCategoryID
WHERE P.ProductSubcategoryID = @ProductSubcategoryID
AND SubCat.ProductCategoryID = @ProductCategoryID
When a dataset that refers to a new query parameter is saved, such as in these examples, a new report
parameter with the same name is automatically created A mapping between the corresponding query
parameter and report parameter is also established on the Dataset Parameters tab
Configure a report parameter by right-clicking on the parameter in the Report Data pane and
choosing Parameter Properties Figure 73-5 shows the Report Parameter Properties dialog with the
ProductSubcategoryIDparameter selected
Follow these steps to configure the parameters:
1 Select theProductCategoryIDparameter
2 Change the prompt to something user-friendly, such as ‘‘Category’’.
Trang 93 Change the data type toInteger Select the Allow null value option.
4 Switch to the Available Values tab, and select ‘‘Get values from a query,’’ which will populate
the available categories from the dataset you defined earlier
5 Select theCategoryListdataset
6 From the Value Field drop-down list, selectProductCategoryID
7 From the Label Field drop-down list, selectName
8 Repeat this process for theProductSubcategoryIDreport parameter, using the Prompt
‘‘SubCategory’’,SubcategoryListas the dataset,ProductSubcategoryIDas the value field, andNameas the label field
9 Make sure that theProductCategoryIDparameter is listed beforeProductSubcategoryID
in the Report Data pane Because the subcategory depends on the category, it must be defined first If the order is not correct, then highlight one of the entries and use the up and down arrows in the Report Data pane’s toolbar to adjust the order
FIGURE 73-5
The Report Parameter Properties dialog controls report parameters
Preview the report Note that theSubCategoryparameter is disabled until you select a value from the
Categoryparameter Once you select a category, theSubCategoryparameter is enabled and contains
the list of subcategories for the selected category Click the View Report button to return only the
prod-ucts within the selected subcategory
Trang 10Multi-value report parameters
The previous example demonstrated how to build a report with nested parameters Let’s enhance the
report now to enable the user to select multiple values from each parameter and include all products
matching the criteria
Changing the parameters to multi-value parameters requires some minor modifications to the SQL for
theSubcategoryListandProductsdatasets Specifically, theWHEREclause must change to use the
INstatement instead of "=" Update the SQL for theSubcategoryListdataset as follows:
SELECT ProductSubcategoryID, ProductCategoryID, Name
FROM Production.ProductSubcategory
WHERE ProductCategoryID IN (@ProductCategoryID)
ORDER BY Name
Update theProductsdataset:
SELECT P.ProductID, P.[Name], P.ProductNumber, P.MakeFlag,
P.Color, P.[Size], P.ListPrice, P.StandardCost,
P.ProductSubcategoryID, SubCat.[Name] AS Subcategory,
SubCat.ProductCategoryID, Cat.[Name] AS Category
FROM Production.Product P
INNER JOIN Production.ProductSubcategory SubCat
ON P.ProductSubcategoryID = SubCat.ProductSubcategoryID INNER JOIN Production.ProductCategory Cat
ON SubCat.ProductCategoryID = Cat.ProductCategoryID WHERE P.ProductSubcategoryID IN (@ProductSubcategoryID)
AND SubCat.ProductCategoryID IN (@ProductCategoryID)
At execution time, Reporting Services will replace the multi-value parameter with a list of values selected
by the user For example,IN (@ProductSubcategoryID)might becomeIN (1,3,7)when the
SQL is executed
Follow these steps to configure the parameters as multi-value parameters:
1 Right-click on theProductCategoryIDparameter in the Report Data pane, select the
‘‘Allow multiple values’’ check box, and click OK to save the changes
2 Right-click on theProductSubcategoryIDparameter in the Report Data pane, select the
‘‘Allow multiple values’’ check box, and click OK to save the changes
Now run the report and select Accessories and Clothing from theCategoryparameter The
Subcategoryparameter now contains all subcategories for Accessories and Clothing, as shown in
Figure 73-6 Select several subcategories and run the report It should now contain all products for the
selected subcategories The report is no longer limited to the selection of a single parameter value
Some caution should be used when designing reports that use multi-value parameters, because every
value selected is passed in as part of the SQL Query In this example, selecting all values will only grow
the size of the query by about 100 characters (a short list of integer IDs is passed in); but other reports
could have many more values and/or longer text strings, which could lead to extremely large queries
and poor performance