Está en la página 1de 79

Chapter 16: SQL Server

Integration Services
In this chapter:

The Import and Export Wizard


Creating a Package
Working with Connection Managers
Building Data Flows
Building Control Flows
Creating Event Handlers
Saving and Running Packages

Files needed:

ISProject1.zip
ISProject2.zip

SQL Server Integration Services


Microsoft says that SQL Server Integration Services (SSIS) is a platform for building
high performance data integration solutions, including extraction, transformation,
and load (ETL) packages for data warehousing. A simpler way to think of SSIS is
that its the solution for automating data movements. SSIS provides a way to build
packages made up of tasks that can move data around from place to place and alter
it on the way. There are visual designers (hosted within Business Intelligence
Development Studio) to help you build these packages as well as an API for
programming SSIS objects from other applications.
In this chapter, youll see how to build and use SSIS packages. First, though, well
look at a simpler facet of SSIS: The SQL Server Import and Export Wizard.
If you choose to use the supplied solution files rather than building
your own, you may need to edit the properties of the OLE DB
Connection Managers within the projects to point to your own test
server. Youll learn more about Connection Managers in the
Working with Connection Managers section later in this chapter.

The Import and Export Wizard


Though SSIS is almost infinitely customizable, Microsoft has produced a simple
wizard to handle some of the most common ETL tasks: importing data to or
exporting data from a SQL Server database. The Import and Export Wizard protects
you from the complexity of SSIS while allowing you to move data between any of
these data sources:

SQL Server databases


Flat files
Microsoft Access databases
Microsoft Excel worksheets
Other OLE DB providers

You can launch the Import and Export wizard from the Tasks entry on the shortcut
menu of any database in the Object Explorer window of SQL Server Management
Studio.

Try It!
To import some data using the Import and Export Wizard, follow these steps:
1. Launch SQL Server Management Studio and log in to your test server.
16-2

Introduction to SQL Server 2008

Copyright 2009 Accelebrate, Inc

The Import and Export Wizard


2. Open a new query window.
3. Select the master database from the Available Databases combo box on the
toolbar.
4. Enter this text into the query window:
CREATE DATABASE Chapter16
5.
6.
7.
8.
9.

Click the Execute toolbar button to create a new database.


Expand the Databases node in Object Explorer
Right-click on the Chapter16 database and select Tasks f Import Data.
Read the first page of the Import and Export Wizard and click Next.
Select SQL Native Client for the data source and provide login information for
your test server.
10. Select the AdventureWorks2008 database as the source of the data to import.
11. Click Next.
12. Because youre importing data, the next page of the wizard will default to
connection information for the Chapter16 database. Click Next.
13. Select Copy Data From One or More Tables or Views and click Next. Note that
if you only want to import part of a table you can use a query as the data
source instead.
14. Select the HumanResources.Department,
HumanResources.JobCandidate and HumanResources.Shift tables, as
show in Figure 16-1. As you select tables, the wizard will automatically assign
names for the target tables.

Copyright 2009 Accelebrate, Inc.

Introduction to SQL Server 2008

16-3

SQL Server Integration Services

Figure 16-1: Selecting tables to import

15. Select the HumanResources.Shift table and click on the Edit Mappings
button.
16. The Column Mappings dialog box lets you change the name, data type, and
other properties of the destination table columns. You can also set other options
here, such as whether to overwrite or append data when importing data to an
existing table. Click Cancel when youre done inspecting the options.
17. Click Next.
18. Check Execute Immediately and click Next.
19. Click Finish to perform the import. SQL Server will display progress as it
performs the import, as shown in Figure 16-2.

16-4

Introduction to SQL Server 2008

Copyright 2009 Accelebrate, Inc

The Import and Export Wizard

Figure 16-2: Import Wizard results

20. Click Close to dismiss the report.


21. Expand the Tables node of the Chapter16 database to verify that the import
succeeded.
In addition to executing its operations immediately, the Import
and Export Wizard can also save a package for later execution.
Youll learn more about packages in the remainder of this chapter.

Copyright 2009 Accelebrate, Inc.

Introduction to SQL Server 2008

16-5

SQL Server Integration Services

Creating a Package
The Import and Export Wizard is easy to use, but it only taps a small part of the
functionality of SSIS. To really appreciate the full power of SSIS, youll need to use
BIDS to build an SSIS package. A package is a collection of SSIS objects including:

Connections to data sources.


Data flows, which include the sources and destinations that extract and load
data, the transformations that modify and extend data, and the paths that link
sources, transformations, and destinations.
A Control flow, which includes tasks and containers that execute when the
package runs. You can organize tasks in sequences and in loops.
Event handlers, which are workflows that runs in response to the events raised
by a package, task, or container.

Youll see how to build each of these components of a package in later sections of the
chapter, but first, lets fire up BIDS and create a new SSIS package.

Try It!
To create a new SSIS package, follow these steps:
1.
2.
3.
4.
5.
6.

Launch Business Intelligence Development Studio


Select File f New f Project.
Select the Business Intelligence Projects project type.
Select the Integration Services Project template.
Select a convenient location.
Name the new project ISProject1 and click OK.

Figure 16-3 shows the new, empty package.

16-6

Introduction to SQL Server 2008

Copyright 2009 Accelebrate, Inc

Working with Connection Managers

Figure 16-3: Empty SSIS package

Working with Connection Managers


SSIS uses connection managers to integrate different data sources into packages.
SSIS includes a wide variety of different connection managers that allow you to
move data around from place to place. Table 16-1 lists some of the available
connection managers.
Connection Manager
ADO
ADO.NET
CACHE
MSOLAP100
EXCEL
FILE
FLATFILE
FTP
HTTP
Copyright 2009 Accelebrate, Inc.

Handles
Connecting to ADO objects such as a
Recordset.
Connecting to data sources through an
ADO.NET provider.
Connects to a cache either in memory or in a file
Connecting to an Analysis Services database or
cube.
Connecting to an Excel worksheet.
Connecting to a file or folder.
Connecting to delimited or fixed width flat files.
Connecting to an FTP data source.
Connecting to an HTTP data source.

Introduction to SQL Server 2008

16-7

SQL Server Integration Services


MSMQ
MULTIFILE
MULTIFLATFILE
ODBC
OLEDB
SMOSever
SMTP
SQLMobile
WMI

Connecting to a Microsoft Message Queue.


Connecting to a set of files, such as all text files
on a particular hard drive.
Connecting to a set of flat files.
Connecting to an ODBC data source.
Connecting to an OLE DB data source.
Connecting to a server via SMO.
Connecting to a Simple Mail Transfer Protocol
server.
Connecting to a SQL Server Mobile database.
Connecting to Windows Management
Instrumentation data.
Table 16-1: Available Connection Managers

To create a Connection Manager, you right-click anywhere in the Connection


Managers area of a package in BIDS and choose the appropriate shortcut from the
shortcut menu. Each Connection Manager has its own custom configuration dialog
box with specific options that you need to fill out.

Try It!
To add some connection managers to your package, follow these steps:
1. Right-click in the Connection Managers area of your new package and select
New OLE DB Connection.
2. Note that the configuration dialog box will show the data connections that you
created in Chapter 15; data connections are shared across Analysis Services and
Integration Services projects. Click New to create a new data connection.
3. In the Connection Manager dialog box, select the SQL Native Client provider.
4. Select your test server and provide login information.
5. Select the Chapter16 database.
6. Click OK.
7. In the Configure OLE DB Connection Manager dialog box, click OK.
8. Right-click in the Connection Managers area of your new package and select
New Flat File Connection.
9. Enter DepartmentList as the Connection Manager Name.
10. Enter C:\Departments.txt as the File Name.
11. Check the Column Names in the First Data Row checkbox. Figure 16-4 shows
the completed General page of the dialog box.

16-8

Introduction to SQL Server 2008

Copyright 2009 Accelebrate, Inc

Try It!

Figure 16-4: Defining a Flat File Connection Manager

12. Click the Advanced icon to move to the Advanced page of the dialog box.
13. Click the New button.
14. Change the Name of the new column to DepartmentName.
15. Click OK.
16. Right-click the DepartmentList Connection Manager and select Copy.
17. Right-click in the Connection Managers area and select Paste.
18. Click on the new DepartmentList 1 connection to select it.
19. Use the Properties Window to change properties of the new connection.
Change the Name property to DepartmentListBackup. Change the
ConnectionString property to C:\DepartmentsBackup.txt.
Figure 16-5 shows the SSIS package with the three Connection Managers defined.
Copyright 2009 Accelebrate, Inc.

Introduction to SQL Server 2008

16-9

SQL Server Integration Services

Figure 16-5: An SSIS package with two Connection Managers

Building Control Flows


The Control Flow tab of the Package Designer is where you tell SSIS what the
package will do. You create your control flow by dragging and dropping items from
the toolbox to the surface, and then dragging and dropping connections between the
objects. The objects you can drop here break up into four different groups:

16-10

Tasks are things that SSIS can do, such as execute SQL statements or transfer
objects from one SQL Server to another. Table 16-2 lists the available tasks.
Maintenance Plan tasks are a special group of tasks that handle jobs such as
checking database integrity and rebuilding indexes. Table 16-3 lists the
maintenance plan tasks.
The Data Flow Task is a general purpose task for ETL (extract, transform, and
load) operations on data. Theres a separate design tab for building the details
of a Data Flow Task.
Containers are objects that can hold a group of tasks. Table 16-4 lists the
available containers.

Introduction to SQL Server 2008

Copyright 2009 Accelebrate, Inc

Building Control Flows


Task

Purpose

ActiveX Script
Analysis Services Execute DDL
Analysis Services Processing
Bulk Insert
Data Mining Query
Data Profiling Task

Execute DTS 2000 Package


Execute Package
Execute Process
Execute SQL
File System
FTP
Message Queue
Script
Send Mail
Transfer Database
Transfer Error Messages
Transfer Jobs
Transfer Logins
Transfer Master Stored Procedures

Transfer SQL Server Objects


Web Service
WMI Data Reader
WMI Event Watcher
XML

Execute an ActiveX Script


Execute DDL query statements against an
Analysis Services server
Process an Analysis Services cube
Insert data from a file into a database
Execute a data mining query
Generate a profile of sample data, determining
distribution of values or percentage of NULLs,
etc.
Execute a Data Transformation Services Package
(DTS was the SQL Server 2000 version of SSIS)
Execute an SSIS package
Shell out to a Windows application
Run a SQL query
Perform file system operations such as copy or
delete
Perform FTP operations
Send or receive messages via MSMQ
Execute a custom task
Send e-mail
Transfer an entire database between two SQL
Servers
Transfer custom error messages between two
SQL Servers
Transfer jobs between two SQL Servers
Transfer logins between two SQL Servers
Transfer stored procedures from the master
database on one SQL Server to the master
database on another SQL Server
Transfer objects between two SQL Servers
Execute a SOAP Web method
Read data via WMI
Wait for a WMI event
Perform operations on XML data

Table 16-2: SSIS control flow tasks

Task
Back Up Database
Check Database Integrity
Execute SQL Server Agent Job
Execute T-SQL Statement
History Cleanup
Maintenance Cleanup
Notify Operator

Copyright 2009 Accelebrate, Inc.

Purpose
Back up an entire database to file or tape
Perform database consistency checks
Run a job
Run any T-SQL script
Clean out history tables for other maintenance
tasks
Clean up files left by other maintenance tasks
Send e-mail to SQL Server operators

Introduction to SQL Server 2008

16-11

SQL Server Integration Services


Rebuild Index
Reorganize Index
Shrink Database
Update Statistics

Rebuild a SQL Server index


Compacts and defragments an index
Shrinks a database
Update statistics used to calculate query plans
Table 16-3: SSIS maintenance plan tasks

Container
For Loop
Foreach Loop
Sequence

Purpose
Repeat a task a fixed number of times
Repeat a task by enumerating over a group of
objects
Group multiple tasks into a single unit for easier
management
Table 16-4: SSIS containers

Try It!
To add control flow tasks to the package youve been building, follow these steps:
1. If the Toolbox isnt visible already, hover your mouse over the Toolbox tab
until it slides out from the side of the BIDS window. Use the pushpin button in
the Toolbox title bar to keep the Toolbox visible.
2. Make sure the Control Flow tab is selected in the Package Designer.
3. Drag a File System Task from the Toolbox and drop it on the Package Designer.
4. Drag a Data Flow Task from the Toolbox and drop it on the Package Designer,
somewhere below the File System task.
5. Click on the File System Task on the Package Designer to select it.
6. Drag the green arrow from the bottom of the File System Task and drop it on
top of the Data Flow Task. This tells SSIS the order of tasks when the File
System Task succeeds.
7. Double-click the connection between the two tasks to open the Precedence
Constraint Editor.
8. Change the Value from Success to Completion, because you want the Data
Flow Task to execute whether the File System Task succeeds or not.
9. Click OK.
10. Select the File System task in the designer. Use the Properties Window to set
properties of the File System Task. Set the Source property to
DepartmentList. Set the Destination property to DepartmentListBackup.
Set the OverwriteDestinationFile property to True then click OK.
Figure 16-6 shows the completed set of control flow tasks.

16-12

Introduction to SQL Server 2008

Copyright 2009 Accelebrate, Inc

Building Data Flows

Figure 16-6: Adding control flow tasks

As it stands, this package uses the file system task to copy the file specified by the
DepartmentList connection to the file specified by the DepartmentListBackup
connection, overwriting any target file that already exists. It then executes the data
flow task. In the next section, youll see how to configure the data flow task.

Building Data Flows


The Data Flow tab of the Package Designer is where you specify the details of any
Data Flow tasks that youve added on the Control Flow tab. Data Flows are made up
of various objects that you drag and drop from the Toolbox:

Data Flow Sources are ways that data gets into the system. Table 16-5 lists the
available data flow sources.
Data Flow Transformations let you alter and manipulate the data in various
ways. Table 16-6 lists the available data flow transformations.
Data Flow Destinations are the places that you can send the transformed data.
Table 16-7 lists the available data flow destinations.

Copyright 2009 Accelebrate, Inc.

Introduction to SQL Server 2008

16-13

SQL Server Integration Services


Source

Use

ADO NET

Extracts data from a database using a .NET data


provider
Extracts data from an Excel workbook
Extracts data from a flat file
Extracts data from a database using an OLE DB
provider
Extracts data from a raw file (proprietary
Microsoft format)
Extracts data from an XML file

Excel
Flat File
OLE DB
Raw File
XML

Table 16-5: Data flow sources

Transformation
Aggregate
Audit
Cache Transform
Character Map
Conditional Split
Copy Column
Data Conversion
Data Mining Query
Derived Column
Export Column
Fuzzy Grouping
Fuzzy Lookup
Import Column
Lookup
Merge
Merge Join
Multicast
OLE DB Command
Percentage Sampling
Pivot
Row Count
Row Sampling
Script Component
Slowly Changing Dimension
Sort
Term Extraction
Term Lookup
Union All
Unpivot

Effect
Aggregates and groups values in a dataset
Adds audit information to a dataset
Populates a CACHE connection manager
Applies string operations to character data
Evaluates and splits up rows in a dataset
Copies a column of data
Converts data to a different datatype
Runs a data mining query
Calculates a new column from existing data
Exports data from a column to a file
Groups rows that contain similar values
Looks up values using fuzzy matching
Imports data from a file to a column
Looks up values in a reference dataset
Merges two sorted datasets
Merges data from two datasets by using a join
Creates copies of a dataset
Executes a SQL command on each row in a
dataset
Extracts a subset of rows from a dataset
Builds a pivot table from a dataset
Counts the rows of a dataset
Extracts a sample of rows from a dataset
Executes a custom script
Updates a slowly changing dimension table
Sorts data
Extracts data from a column
Looks up the frequency of a term in a column
Merges multiple datasets
Normalizes a pivot table

Table 16-6: Data Flow Transformations

16-14

Introduction to SQL Server 2008

Copyright 2009 Accelebrate, Inc

Building Data Flows


Destination
ADO NET
Data Mining Model Training
DataReader
Dimension Processing
Excel
Flat File
OLE DB
Partition Processing
Raw File
Recordset
SQL Server Compact
SQL Server

Use
Sends data to a .NET data provider
Sends data to an Analysis Services data mining
model
Sends data to an in-memory ADO.NET
DataReader
Processes a cube dimension
Sends data to an Excel worksheet
Sends data to a flat file
Sends data to an OLE DB database
Processes an Analysis Services partition
Sends data to a raw file
Sends data to an in-memory ADO Recordset
Sends data to a SQL Server CE database
Sends data to a SQL Server database
Table 16-7: Data Flow Destinations

If you are running SQL Server Integration Services on a 64-bit


machine, the Excel source and destination will throw an exception.
During development, you can select Project f Project_name
Properties, select the Debugging page and change the
Run64BitRuntime property to false. When deploying the package,
youll need to shell out to the 32-bit SSIS runtime when scheduling
the package.

Try It!
To customize the data flow task in the package youre building, follow these steps:
1. Select the Data Flow tab in the Package Designer. The single Data Flow Task in
the package will automatically be selected in the combo box.
2. Drag an OLE DB Source from the Toolbox and drop it on the Package Designer.
3. Drag a Character Map Transformation from the Toolbox and drop it on the
Package Designer.
4. Drag a Flat File Destination from the Toolbox and drop it on the Package
Designer.
5. Click on the OLE DB Source on the Package Designer to select it.
6. Drag the green arrow from the bottom of the OLE DB Source and drop it on top
of the Character Map Transformation.
7. Click on the Character Map Transformation on the Package Designer to select
it.

Copyright 2009 Accelebrate, Inc.

Introduction to SQL Server 2008

16-15

SQL Server Integration Services


8. Drag the green arrow from the bottom of the Character Map Transformation
and drop it on top of the Flat File Destination.
9. Double-click the OLE DB Source to open the OLE DB Source Editor. Notice that
it uses the Chapter16 OLE DB connection manager by default.
10. Select the HumanResources.Department table. Figure 16-7 shows the
completed OLE DB Source Editor.

Figure 16-7: Setting up the OLE DB Source

11. Click OK.


12. Double-click the Character Map Transformation.
13. Check the Name column.
14. Select In-Place Change in the Destination column.
15. Select the Uppercase operation. Figure 16-8 shows the completed Character
Map Transformation Editor.

16-16

Introduction to SQL Server 2008

Copyright 2009 Accelebrate, Inc

Building Data Flows

Figure 16-8: Setting up the Character Map Transformation

16. Click OK.


17. Double-click the Flat File Destination.
18. Select the DepartmentList Flat File Connection Manager.
19. Select the Mappings page of the dialog box.
20. Drag the Name column from the Available Input Columns list and drop it on
top of the DepartmentName column in the Available Destination Columns
list. Figure 16-9 shows the completed Mappings page.

Copyright 2009 Accelebrate, Inc.

Introduction to SQL Server 2008

16-17

SQL Server Integration Services

Figure 16-9: Configuring the Flat File Destination

21. Click OK.


Figure 16-10 shows the completed data flow.

16-18

Introduction to SQL Server 2008

Copyright 2009 Accelebrate, Inc

Creating Event Handlers

Figure 16-10: Adding data flow tasks

The data flow tasks in this package take a table from the Chapter16 database,
transform one of the columns in that table to all uppercase characters, and then write
that transformed column out to a flat file.

Creating Event Handlers


SSIS packages also support a complete event system. You can attach event handlers
to a variety of events for the package itself or for the individual tasks within a
package. Events within a package bubble up. That is, suppose an error occurs
within a task inside of a package. If youve defined an OnError event handler for
the task, then that event handler is called. Otherwise, an OnError event handler for
the package itself is called. If no event handler is defined for the package either, the
event is ignored.
Event handlers are defined on the Event Handlers tab of the Package Designer.
When you create an event handler, you handle the event by building an entire
secondary SSIS package, and you have access to the full complement of data flows,
control flows, and event handlers to deal with the original event.

Copyright 2009 Accelebrate, Inc.

Introduction to SQL Server 2008

16-19

SQL Server Integration Services


By adding event handlers that call the Send Mail task to the
OnError event, you can notify operators by e-mail if anything
goes wrong in the course of running an SSIS package.

Try It!
To add an event handler to the package weve been building, follow these steps:
1. Open SQL Server Management Studio and connect to your test server.
2. Create a new query and select the Chapter16 database in the available
databases list on the toolbar.
3. Enter this text into a query window:
CREATE TABLE DepartmentExports(
ExportID int IDENTITY(1,1) NOT NULL,
ExportTime datetime NOT NULL
CONSTRAINT DF_DepartmentExports_ExportTime DEFAULT
(GETDATE()),
CONSTRAINT PK_DepartmentExports PRIMARY KEY CLUSTERED
(
ExportID ASC
)
)
4.
5.
6.
7.

Click the Execute toolbar button to create the table.


Switch back to the Package Designer in BIDS.
Select the Event Handlers tab.
In the Executable drop-down list, expand the Package node and then the
Executables node.
8. Select the Data Flow Task in the Executable dropdown list, click OK.
9. Select the OnPostExecute event handler.
10. Click the hyperlink on the design surface to create the event handler.
11. Drag an Execute SQL task from the Toolbox and drop it on the Package
Designer.
12. Double-click the Execute SQL task to open the Execute SQL Task Editor.
13. Select the Chapter16 OLE DB connection manager as the tasks connection.
14. Set the SQL Statement property to the following query:
INSERT INTO DepartmentExports (ExportTime)
VALUES (GETDATE())
15. Click OK to create the event handler.

16-20

Introduction to SQL Server 2008

Copyright 2009 Accelebrate, Inc

Saving and Running Packages


This event handler will be called when the Data Flow Task finishes executing, and
will insert one new row into the tracking table when it is called.

Saving and Running Packages


Now that youve created an entire SSIS package, youre probably ready to run it and
see what it does. But first, lets look at the options for saving SSIS packages. When
you work in BIDS, your SSIS package is saved as an XML file (with the extension
dtsx) directly in the normal Windows file system. But thats not the only option.
Packages can also be saved in the msdb database in SQL Server itself, or in a special
area of the file system called the Package Store.
Storing SSIS packages in the Package Store or the msdb database makes it easier to
access and manage them from SQL Servers administrative and command-line tools
without needing to have any knowledge of the physical layout of the servers hard
drive.

Saving Packages to Alternate Locations


To save a package to the msdb database or the Package Store, you use the File f
Save Package As menu item within BIDS.

Try It!
To store copies of the package youve developed, follow these steps.
1. Select File f Save Copy of Package.dtsx As from the BIDS menus.
2. Select SSIS Package Store as the Package Location.
3. Select the name of your test server.
4. Enter /File System/ExportDepartments as the package path.
5. Click OK.
6. Select File f Save Copy of Package.dtsx As from the BIDS menus.
7. Select SQL Server as the Package Location.
8. Select the name of your test server and fill in your authentication information.
9. Enter ExportDepartments as the package path.
10. Click OK.

Running a Package
You can run the final package from either BIDS or SQL Server Management Studio.
When youre developing a package, its convenient to run it directly from BIDS.
Copyright 2009 Accelebrate, Inc.

Introduction to SQL Server 2008

16-21

SQL Server Integration Services


When the package has been deployed to a production server (and saved to the msdb
database or the Package Store) youll probably want to run it from SQL Server
Management Studio.
SQL Server also includes a command-line utility, dtexec, that lets
you run packages from batch files.

Running a Package from BIDS


With the package open in BIDS, you can run it using the standard Visual Studio
tools for running a project. Choose any of these options:

Right-click the package in Solution Explorer and select Execute Package.


Click the Start Debugging toolbar button.
Press F5.

Try It!
To run the package that you have loaded in BIDS, follow these steps:
1. Click the Start Debugging toolbar button. SSIS will execute the package,
highlighting the steps in the package as they are completed. You can select any
tab to watch whats going on. For example, if you select the Control Flow tab,
youll see tasks highlighted, as shown in Figure 16-11.

16-22

Introduction to SQL Server 2008

Copyright 2009 Accelebrate, Inc

Saving and Running Packages

Figure 16-11: Executing a package in the debugger

2. When the package finishes executing, click the hyperlink underneath the
Connection Managers pane to stop the debugger.
3. Click the Execution Results tab to see detailed information on the package, as
shown in Figure 16-12.

Copyright 2009 Accelebrate, Inc.

Introduction to SQL Server 2008

16-23

SQL Server Integration Services

Figure 16-12: Information on package execution

All of the events you see in the Execution Results pane are things
that you can create event handlers to react to within the package.
As you can see, DTS issues a quite a number of events, from
progress events to warnings about extra columns of data that we
retrieved but never used.

Running a Package from SQL Server Management Studio


To run a package from SQL Server Management Studio, you need to connect Object
Browser to SSIS.

Try It!
1. In SQL Server Management Studio, click the Connect button at the top of the
Object Explorer window.
16-24

Introduction to SQL Server 2008

Copyright 2009 Accelebrate, Inc

Try It!
2. Select Integration Services.
3. Choose the server with Integration Services installed and click Connect. This
will add an Integration Services node at the bottom of Object Explorer.
4. Expand the Stored Packages node. Youll see that you can drill down into the
File System node to find packages in the Package Store, or the MSDB node to
find packages stored in the msdb database.
5. Expand the File System node.
6. Right-click on the ExportDepartments package and select Run Package. This
will open the Execute Package utility, shown in Figure 16-13.

Figure 16-13: Executing a package from SQL Server Management Studio

7. Click Execute.
8. Click Close twice to dismiss the progress dialog box and the Execute Package
Utility.
9. Enter this text into a query window with the Chapter16 database selected:
SELECT * FROM DepartmentExports

Copyright 2009 Accelebrate, Inc.

Introduction to SQL Server 2008

16-25

SQL Server Integration Services


10. Click the Execute toolbar button to verify that the package was run. You should
see one entry for when the package was run from BIDS and one from when you
ran it from SQL Server Management Studio.

16-26

Introduction to SQL Server 2008

Copyright 2009 Accelebrate, Inc

Exercises

Exercises
One common use of SSIS is in data warehousing - collecting data from a variety of
different sources into a single database that can be used for unified reporting. In this
exercise youll use SSIS to perform a simple data warehousing task.
Use SSIS to create a text file, c:\EmployeeDept.txt, containing the last names,
department names, start and end dates of the AdventureWorks2008 employees.
Retrieve the last names from the Person.Person table and the department start
and end dates from the HumanResources.EmployeeDepartmentHistory table
in the AdventureWorks2008 database, and the department names from the
Chapter16 database.
You can use the Merge Join data flow transformation to join data from two sources.
One tip: the inputs to this transformation need to be sorted on the joining column.

Copyright 2009 Accelebrate, Inc.

Introduction to SQL Server 2008

16-27

SQL Server Integration Services

Solutions to Exercises
1.
2.
3.
4.
5.
6.
7.

Launch Business Intelligence Development Studio


Select File f New f Project.
Select the Business Intelligence Projects project type.
Select the Integration Services Project template.
Select a convenient location.
Name the new project ISProject2 and click OK.
Right-click in the Connection Managers area of your new package and select
New OLE DB Connection.
8. Click New to create a new data connection.
9. In the Connection Manager dialog box, select the SQL Native Client provider.
10. Select your test server and provide login information.
11. Select the AdventureWorks2008 database.
12. Click OK.
13. Right-click in the Connection Managers area of your new package and select
New OLE DB Connection.
14. Select the existing connection to the Chapter16 database and click OK.
15. Right-click in the Connection Managers area of your new package and select
New Flat File Connection.
16. Enter EmployeeList as the Connection Manager Name.
17. Enter C:\EmployeeDept.txt as the File Name.
18. Check the Column Names in the First Data Row checkbox.
19. Click the Advanced icon to move to the Advanced page of the dialog box.
20. Click the New button.
21. Change the Name of the new column to LastName.
22. Click the New button.
23. Change the Name of the new column to Department.
24. Click the New button.
25. Change the Name of the new column to StartDate and the datatype to Date.
26. Click the New button.
27. Change the Name of the new column to EndDate and the datatype to Date.
28. Click OK.
29. Select the Control Flow tab in the Package Designer.
30. Drag a Data Flow Task from the Toolbox and drop it on the Package Designer.
31. Select the Data Flow tab in the Package Designer. The single Data Flow Task in
the package will automatically be selected in the combo box.
32. Drag an OLE DB Source from the Toolbox and drop it on the Package Designer.
33. Drag a second OLE DB Source from the Toolbox and drop it on the Package
Designer.
34. Drag a Sort Transformation from the Toolbox and drop it on the Package
Designer.
16-28

Introduction to SQL Server 2008

Copyright 2009 Accelebrate, Inc

Solutions to Exercises
35. Drag a second Sort Transformation from the Toolbox and drop it on the
Package Designer.
36. Drag a Merge Join Transformation from the Toolbox and drop it on the
Package Designer.
37. Drag a Flat File Destination from the Toolbox and drop it on the Package
Designer.
38. Click on the first OLE DB Source on the Package Designer to select it.
39. Drag the green arrow from the bottom of the first OLE DB Source and drop it
on top of the first Sort Transformation.
40. Click on the second OLE DB Source on the Package Designer to select it.
41. Drag the green arrow from the bottom of the second OLE DB Source and drop
it on top of the second Sort Transformation.
42. Click on the first Sort Transformation on the Package Designer to select it.
43. Drag the green arrow from the bottom of the first Sort Transformation and
drop it on top of the Merge Join Transformation.
44. In the Input Output Selection dialog box, select Merge Join Left Input.
45. Click OK.
46. Click on the second Sort Transformation on the Package Designer to select it.
47. Drag the green arrow from the bottom of the second Sort Transformation and
drop it on top of the Merge Join Transformation.
48. Click on the Merge Join Transformation on the Package Designer to select it.
49. Drag the green arrow from the bottom of the Merge Join Transformation and
drop it on top of the Flat File Destination. Figure 16-14 shows the Data Flow tab
with the connections between tasks.

Copyright 2009 Accelebrate, Inc.

Introduction to SQL Server 2008

16-29

SQL Server Integration Services

Figure 16-14: Data flow tasks to merge two sources

50. Double-click the first OLE DB Source to open the OLE DB Source Editor.
51. Select the connection to the AdventureWorks2008 database.
52. For the Data Access Mode, select SQL Command.
53. Enter the following query:
SELECT p.LastName, dh.DepartmentID,
dh.StartDate, dh.EndDate
FROM
Person.Person p
INNER JOIN HumanResources.EmployeeDepartmentHistory dh
ON p.BusinessEntityID = dh.BusinessEntityID
54. Click OK.
55. Double-click the second OLE DB Source to open the OLE DB Source Editor.
56. Select the connection to the Chapter16 database.
57. Select the HumanResources.Department table.
58. Click OK.
59. Double-click the first Sort Transformation.
60. Check the DepartmentID column.
61. Click OK
16-30

Introduction to SQL Server 2008

Copyright 2009 Accelebrate, Inc

Solutions to Exercises
62. Double-click the second Sort Transformation.
63. Check the DepartmentID column.
64. Click OK
65. Double-click the Merge Join Transformation.
66. Check the Join Key checkbox for the ContactID column in both tables.
67. Check the selection checkbox for the LastName, StartDate and EndDate
columns in the left-hand table and the Name column in the right-hand table;
alias the Name column as DepartmentName. Figure 16-15 shows the
completed Merge Join Transformation Editor.

Figure 16-15: Setting up a Merge Join

68. Click OK.


69. Double-click the Flat File Destination.
70. Select the EmployeeList Flat File Connection Manager.
Copyright 2009 Accelebrate, Inc.

Introduction to SQL Server 2008

16-31

SQL Server Integration Services


71. Select the Mappings page of the dialog box.
72. The LastName, StartDate and EndDate columns will be automatically
mapped. Drag the DepartmentName column from the Available Input
Columns list and drop it on top of the Department column in the Available
Destination Columns list.
73. Click OK.
74. Right-click the package in Solution Explorer and select Execute Package.
75. Stop debugging when the package is finished executing.
76. Open the c:\EmployeeDept.txt file to inspect the results.

16-32

Introduction to SQL Server 2008

Copyright 2009 Accelebrate, Inc

Chapter 17: Reporting


Services
In this chapter:

The Reporting Services Architecture


Using Report Designer
Publishing a Report
Using Report Builder
Using Report Manager

Files needed:

ProductReport1.zip
ProductReport2.zip
AWSales.zip

Reporting Services
For many years, SQL Server did not have a good answer for creating attractive
reports that summarize information in ways that make sense to business users.
Finally, Microsoft shipped SQL Server Reporting Services. Like Notification
Services, Reporting Services was originally an add-on for SQL Server 2000, and now
its a part of the core product. In this chapter, youll learn how to use Reporting
Services to produce your own reports.

The Reporting Services Architecture


Reporting Services has a quite a few components that work together seamlessly to
provide a complete reporting solution. The full Reporting Services architecture
includes development tools, administration tools, and report viewers. There are a
number of ways to get to Reporting Services programmatically, including URL,
SOAP and WMI interfaces.
Figure 17-1 shows a simplified diagram of the main Reporting Services components
that well be using in this chapter.

Figure 17-1: Report Server architecture

In this chapter youll learn about these components:

17-2

Report Server is the core engine that drives Reporting Services.


Report Manager is a Web-based administrative interface for Reporting
Services.
Report Designer is a developer tool for building complex reports.
Report Builder is a simplified end-user tool for building reports.
Introduction to SQL Server 2008

Copyright 2009 Accelebrate, Inc

Using Report Designer

The Report Server database stores report definitions. Reports themselves can
make use of data from many different data sources.

Using Report Designer


Reporting Services includes two tools for creating reports:

Report Designer can create reports of any complexity that Reporting Services
supports, but requires you to understand the structure of your data and to be
able to navigate the Visual Studio user interface.
Report Builder provides a simpler user interface for creating ad hoc reports,
directed primarily at business users rather than developers. Report Builder
requires a developer or administrator to set up a data model before end users
can create reports.

Well start our tour of Reporting Services with Report Designer. Report Designer
runs inside the Business Intelligence Development Studio shell, and offers several
ways to create reports. You can either use the Report Wizard to quickly create a
report, or you can use a set of design tools to build a report from scratch. You can
also use the design tools to modify a report created with the wizard.

Using the Report Wizard


The easiest way to create a report in Report Designer is to use the Report Wizard.
Like all wizards, the Report Wizard walks you through the process in step-by-step
fashion. You can make the following choices in the wizard:

The data source to use


The query to use to retrieve data
Whether to use a tabular or matrix layout for the report
How to group the retrieved data
What visual style to use
Where to deploy the finished report

Try It!
To create a simple report using the Report Wizard, follow these steps:
1.
2.
3.
4.

Launch Business Intelligence Development Studio.


Select File f New f Project.
Select the Business Intelligence Projects project type.
Select the Report Server Project Wizard template.

Copyright 2009 Accelebrate, Inc.

Introduction to SQL Server 2008

17-3

Reporting Services
5. Name the new project ProductReport1 and pick a convenient location to save it
in.
6. Click OK.
7. Read the first page of the Report Wizard and click Next.
8. Name the new data source AdventureWorksDS.
9. Click the Edit button.
10. Log on to your test server.
11. Select the AdventureWorks2008 database.
12. Click OK.
13. Click the Credentials button.
14. Select Use Windows Authentication.
15. Click OK.
16. Check the Make This a Shared Data Source checkbox. This will make this
particular data source available to other Reporting Services applications in the
future.
17. Click Next.
18. Click the Query Builder button.
19. If the full query designer interface does not display by default, click the query
designer toolbar button at the far left end of the toolbar. Figure 17-2 shows the
full query designer interface.

17-4

Introduction to SQL Server 2008

Copyright 2009 Accelebrate, Inc

Using Report Designer


Figure 17-2: Query Builder

20. Click the Add Table toolbar button.


21. Select the Product table and click Add.
22. Click Close.
23. Check the Name, ProductNumber, Color, and ListPrice columns.
24. Click OK.
25. Click Next.
26. Select the Tabular layout and click Next.
27. Move the Color column to the Group area, and the other three columns to the
Detail area, as shown in Figure 17-3.

Figure17-3: Grouping columns in the report

28. Click Next.


Copyright 2009 Accelebrate, Inc.

Introduction to SQL Server 2008

17-5

Reporting Services
29. Select the Stepped layout and click Next.
30. Select the Ocean style and click Next.
31. Accept the default deployment location and click Next.
32. Name the report ProductReport1.
33. Check the Preview Report checkbox.
34. Click Finish.
Figure 17-4 shows the finished report, open in Report Designer.

Figure 17-4: Report created by the Report Wizard

Figure 17-4 shows the main features of Report Designer:

17-6

The Report Data window shows the data that is available to the report.
The main design window lets you view the report itself. You can see a
preview of the report, work with the report in a layout designer, or work with
the query that returns the data for the report.
The Solution Explorer, Output, and Properties windows are the standard
Visual Studio windows.

Introduction to SQL Server 2008

Copyright 2009 Accelebrate, Inc

Using Report Designer

Modifying a Report
Now that youve created a report with the Report Wizard, you can modify it with
the Report Designer. If youve used any sort of visual report design tool in the past,
you should have no problem making changes here. Among the possibilities here:

You can change the available data or the sort order for the report by
modifying the query.
You can resize or rearrange controls on the Design tab.
You can use the Properties window to change properties of individual
controls including their font, alignment, colors, and so on.

Try It!
To modify the report that you just created, follow these steps:
1. Click the Design tab to make the report editable.
2. In the Report Data window, right-click on DataSet1 and select Dataset
Properties.
3. In the Dataset Properties window, click the Query Designer button.
4. Select a Descending sort type for the ListPrice column and click OK.
5. Click OK.
6. Click in the textbox at the top of the report, where the report name is displayed.
7. Click a second time in the textbox to put it in edit mode and change the value
of this control to Products By Color.
8. Click on the header for the Product column.
9. Place the cursor between the column selectors above the Name and Product
Number columns to display a double-headed arrow. Hold down the mouse
button and drag the cursor to the right to widen the Name column.
10. Place the cursor between the column selectors above the Product Number
and ListPrice columns to display a double-headed arrow. Hold down the
mouse button and drag the cursor to the right to widen the Product Number
column.
11. Click on the Preview tab to view the modified report, as shown in Figure 17-5.

Copyright 2009 Accelebrate, Inc.

Introduction to SQL Server 2008

17-7

Reporting Services

Figure 17-5: Modified product report

Designing a Report From Scratch


You can also use Report Designer to build your own reports starting from scratch. In
general, youll follow these steps to create a report:
1. Create a Report project in Business Intelligence Design Studio or open an
existing Report project.
2. Add a report to the project.
3. Create one or more datasets for the report.
4. Build the report layout.

Try It!
To create a fresh report in Report Designer, follow these steps:
1. Select File f New f Project.
17-8

Introduction to SQL Server 2008

Copyright 2009 Accelebrate, Inc

Using Report Designer


2. Select the Business Intelligence Projects project type.
3. Select the Report Server Project template.
4. Name the new report ProductReport2 and pick a convenient location to save
it in.
5. Right-click on the Reports node in Solution Explorer and select Add f New
Item.
6. Select the Report template.
7. Name the new report ProductReport2.rdl and click Add.
8. In the Report Data window, select New f Data Source.
9. Name the new Data Source AdventureWorksDS.
10. Select the Embedded Connection option and click on the Edit button.
11. Connect to your test server and choose the AdventureWorks2008 database.
12. Click OK.
13. Click OK again to create the data source.
14. In the Report Data window, select New f Dataset.
15. Name the dataset dsLocation.
16. Click the Query Designer button.
17. If the full Query Designer does not appear, click on the Edit As Text button.
18. Click the Add Table button.
19. Select the Location table.
20. Click Add.
21. Click Close.
22. Check the boxes for the Name and CostRate columns.
23. Sort the dataset in ascending order by Name and click OK.
24. Click OK again to create the dataset.
25. Open the toolbox window (View f Toolbox).
26. Double-click the Table control.
27. Switch back to the Report Data window.
28. Expand the dataset to show the column names.
29. Drag the Name field and drop it in the first column of the table control on the
design tab.
30. Drag the CostRate field from the Report Data window and drop it in the
second column of the table control.
31. Place the cursor between the column selectors above the Name and CostRate
columns to display a double-headed arrow. Hold down the mouse button and
drag the cursor to the right to widen the Name column.
32. Figure 17-6 shows the report in Design view.

Copyright 2009 Accelebrate, Inc.

Introduction to SQL Server 2008

17-9

Reporting Services

Figure 17-6: Designing a report from scratch

33. Select the Preview tab to see the report with data.

Publishing a Report
Creating reports in Business Intelligence Development Studio is good for
developers, but it doesnt help users at all. In order for the reports you build to be
available to others, you must publish them to your Reporting Services server. To
publish a report, you can use the Build and Deploy menu items in Business
Intelligence Development Studio. Before you do this, you need to check the projects
configuration to make sure that youve selected an appropriate server for the
deployment.

17-10

Introduction to SQL Server 2008

Copyright 2009 Accelebrate, Inc

Publishing a Report

Try It!
You can publish any report, but the first report you created is probably more
visually interesting at this point. To publish the first report, follow these steps:
1.
2.
3.
4.

Select File f Recent Projects and choose your ProductReport1 project.


Select Project f ProductReport1 Properties.
Click the Configuration Manager button.
Fill in the Target Server URL for your Report Server. If youre developing on
the same computer where Reporting Services is installed, and you installed in
the default configuration, this will be http://localhost/ReportServer.
Figure 17-7 shows the completed Property Pages.

Figure 17-7: Setting report project properties

5. Click OK.
6. Select Build f Deploy ProductReport1. The Output Window will track the
progress of BIDS in deploying your report, as shown in Figure 17-8. Depending
on the speed of your computer, building the report may take some time.

Copyright 2009 Accelebrate, Inc.

Introduction to SQL Server 2008

17-11

Reporting Services

Figure 17-8: Deploying a report

7. Launch a web browser and enter the address http://localhost/reports.


8. It may take several minutes for the web page to display; Reporting Services
goes to sleep when it hasnt been used for a while and can take a while to spin
up to speed. Figure 17-9 shows the result.

17-12

Introduction to SQL Server 2008

Copyright 2009 Accelebrate, Inc

Using Report Builder


Figure 17-9: The Report Manager.

9. Click the link for the ProductReport1 folder.


10. Click the link for the ProductReport1 report.

Using Report Builder


Report Designer gives you one way to create reports for Reporting Services, but its
not the only way. SQL Server 2008 also includes a tool directed at end users named
Report Builder. Unlike Report Designer, which is aimed at Developers, Report
Builder presents a simplified view of the report-building process and is intended for
business analysts and other end users.

Building a Data Model


Report Builder doesnt let end users explore all of a SQL Server database. Instead, it
depends on a data model: a preselected group of tables and relationships that a
developer has identified as suitable for end-user reporting. To build a data model,
you use Business Intelligence Development Studio. Data models contain three
things:

Data Sources connect the data model to actual data.


Data Source Views draw data from data sources.
Report Models contain entities that end users can use on reports.

Try It!
To create a data model, follow these steps:
1.
2.
3.
4.
5.
6.
7.

If its not already open, launch Business Intelligence Development Studio


Select File f New f Project.
Select the Business Intelligence Projects project type.
Select the Report Model Project template.
Name the new project AWSales and save it in a convenient location.
Click OK.
Right-click on Data Sources in Solution Explorer and select Add New Data
Source.
8. Read the first page of the Add New Data Source Wizard and click Next.
9. Click New.
10. In the Connection Manager dialog box connect to the AdventureWorks2008
database on your test server and click OK.
11. Click Next.
12. Name the new data source AdventureWorks and click Finish.

Copyright 2009 Accelebrate, Inc.

Introduction to SQL Server 2008

17-13

Reporting Services
13. Right-click on Data Source Views in Solution Explorer and select Add New
Data Source View.
14. Read the first page of the Add New Data Source View Wizard and click Next.
15. Select the AdventureWorks data source and click Next.
16. Select the Product(Production) table and click the > button to move it to
the Included Objects listbox.
17. Select the SalesOrderDetail(Sales) table and click the > button to move
it to the Included Objects listbox.
18. Click the Add Related Tables button.
19. Click Next.
20. Click Finish.
21. Right-click on Report Models in Solution Explorer and select Add New Report
Model.
22. Read the first page of the Report Model Wizard and click Next.
23. Select the Adventure Works2008 data source view and click Next.
24. Keep the default rules selection, as shown in Figure 17-10, and click Next.

17-14

Introduction to SQL Server 2008

Copyright 2009 Accelebrate, Inc

Using Report Builder

Figure 17-10: Creating entities for end-user reporting

25. Choose the Update Statistics option and click Next.


26. Click Run to complete the wizard.
27. Click Finish. If you get a warning that a file was modified outside the source
editor, click Yes.
28. Select Build f Deploy AWSales to deploy the report model to the local
Reporting Services server.

Building a Report
Report Builder itself is a ClickOnce Windows Forms application. That means that its
a Windows application that end users launch from their web browser, but it never
gets installed on their computer, so they dont need any local administrator rights on
their computer to run it. To get started with Report Builder, browse to your
Reporting Services home page. Typically, this will have a URL such as
Copyright 2009 Accelebrate, Inc.

Introduction to SQL Server 2008

17-15

Reporting Services
http://ServerName/Reports (or http://localhost/Reports if you're
running the browser on the same box with SQL Server 2008 itself). Figure 17-11
shows the Reporting Services home page.

Figure 17-11: Reporting Services home page

To run Report Builder, click the Report Builder link in the home page menu bar.
Report Builder will automatically load up all of the available report models and wait
for you to choose one to build a report from.

Try It!
1. Open a browser window and navigate to http://localhost/Reports (or
to the appropriate Report Server URL if youre not working on the report
server).
2. Click the Report Builder link.
3. Depending on your operating system, you may have to confirm that you want
to run the application.

17-16

Introduction to SQL Server 2008

Copyright 2009 Accelebrate, Inc

Using Report Builder


4. After Report Builder is loaded, select the AdventureWorks2008 report model
and the table report layout. Click OK. Figure 17-12 shows the new blank report
that Report Builder will create.

Figure 17-12: New report in Report Builder

The Explorer window to the left of the design surface shows all of
the tables in the report model. Beneath that, the Fields window
shows the attributes in the currently-selected entity. Note that not
everything in this window is a column in the table: the report
model also contains aggregate entities such as Total Safety Stock
Level and automatically calculated fields.
5. Select the Product table.
6. Drag the Name field and drop it in the area labeled Drag and Drop Column
Fields.
7. Click on Special Offer Products in the Explorer window to show related
child tables.
8. Click on Sales Order Details.
9. Drag the Total Order Qty field and drop it to the right of the Name field.
10. Click where it says Click to Add Title and type Product Sales.
Copyright 2009 Accelebrate, Inc.

Introduction to SQL Server 2008

17-17

Reporting Services
11. Click the Run Report button to produce the report shown in Figure 17-13.

Figure 17-13: Report in Report Builder

12. Click the Sort and Group toolbar button.


13. Select to sort by Total Order Qty descending.
14. Click OK.
15. Select File f Save.
16. Name the new report Product Sales.
17. Click Save. This will publish the report back to the Reporting Services server
that you originally downloaded Report Builder from.

17-18

Introduction to SQL Server 2008

Copyright 2009 Accelebrate, Inc

Using Report Manager

Using Report Manager


The Web home page for Reporting Services provides a complete interface for
managing reports (as well as other objects such as data sources and models) after
they are created. This interface, known as Report Manager, is intended primarily for
database administrators, but as a developer you should know about its capabilities
for managing and modifying reports.
When you click on a report in Report Manager, youll see the reports data, as shown
in Figure 17-14.

Figure 17-14: Report in Report Manager

Note that reports in Report Manager open in a tabbed interface. The four tabs allow
you to perform various functions:

View allows you to see the current data in the report.

Copyright 2009 Accelebrate, Inc.

Introduction to SQL Server 2008

17-19

Reporting Services

Properties lets you adjust such things as the reports name, data source,
security credentials, caching, and end-user security.
History shows you saved snapshots of the report.
Subscriptions lets you create subscriptions to the report. Subscriptions allow
you to set up periodic delivery of reports to end users by e-mail or file share.

Printing and Exporting Reports


When viewing reports in the Report Manager, users can print the reports directly
from their browser. The print button in the report toolbar utilizes an ActiveX control
for client-side printing. The first time this button is clicked on a given computer, the
user is prompted to install the ActiveX control, as in Figure 17-15. After that, the
standard Windows print dialog box is displayed for the user to select a printer and
paper size, etc.

Figure 17-15: ActiveX install prompt.

Users can also export the report into any of several handy formats. Table 17-1 lists
the available export formats.
Export Format
XML
CSV
PDF
MHTML
EXCEL
TIFF
Word

Handles
Creates a data file in XML format.
Creates a comma-delimited text file of report data.
Creates an Adobe Acrobat file with the formatted report.
Creates a Web Archive file with the formatted report.
Creates a MS Excel spreadsheet with the formatted
report.
Creates a TIFF graphic of the formatted report.
Creates a MS Word document with the formatted report.
Table 17-1: Export Formats

17-20

Introduction to SQL Server 2008

Copyright 2009 Accelebrate, Inc

Exercises

Exercises
Use Report Builder to create a report from the AdventureWorks2008 data model
showing the minimum and maximum order quantity for orders taken by each
salesperson in the company. Youll find the necessary data in the
SalesOrderHeader and SalesOrderDetail tables.

Copyright 2009 Accelebrate, Inc.

Introduction to SQL Server 2008

17-21

Reporting Services

Solutions to Exercises
1. Open a browser window and navigate to http://localhost/Reports (or
to the appropriate Report Server URL if youre not working on the report
server).
2. Click the Report Builder link.
3. Select the AdventureWorks2008 report model and the table report layout.
4. Click OK.
5. Select the Sales Order Header table.
6. Drag the Sales Person ID field and drop it in the area labeled Drag and
Drop Column Fields.
7. Click on Sales Order Details in the Explorer window.
8. Expand the Total Order Qty field in the Fields window to show the
alternative fields beneath it.
9. Drag the Min Order Qty field and drop it to the right of the Name field.
10. Drag the Max Order Qty field and drop it to the right of the Min Order
Qty field.
11. Click where it says Click to Add Title and type Sales Performance.
12. Click the Run Report button to produce the report shown in Figure 17-15.

17-22

Introduction to SQL Server 2008

Copyright 2009 Accelebrate, Inc

Solutions to Exercises

Figure 17-15: Sales performance report

Copyright 2009 Accelebrate, Inc.

Introduction to SQL Server 2008

17-23

Chapter 18: Reporting


Services
In this chapter:

The Reporting Services Architecture


Using Report Designer
Publishing a Report
Using Report Builder
Using Report Manager

Files needed:

ProductReport1.zip
ProductReport2.zip
AWSales.zip

Reporting Services
For many years, SQL Server did not have a good answer for creating attractive
reports that summarize information in ways that make sense to business users.
Finally, Microsoft shipped SQL Server Reporting Services. Like Notification
Services, Reporting Services was originally an add-on for SQL Server 2000, and now
its a part of the core product. In this chapter, youll learn how to use Reporting
Services to produce your own reports.

The Reporting Services Architecture


For a relatively new product, Reporting Services has a quite complex architecture.
The full Reporting Services architecture includes development tools, administration
tools, and report viewers. There are a number of ways to get to Reporting Services
programmatically, including SOAP and WMI interfaces.
Figure 18-1 shows a simplified diagram of the main Reporting Services components
that well be using in this chapter.

Figure 18-1: Report Server architecture

In this chapter youll learn about these components:

Report Server is the core engine that drives Reporting Services.


Report Manager is a Web-based administrative interface for Reporting
Services.
Report Designer is a developer tool for building complex reports.
Report Builder is a simplified end-user tool for building reports.

Copyright 2005 Accelebrate, Inc

Introduction to SQL Server 2005


18-2

http://www.accelebrate.com

Using Report Designer

The Report Server database stores report definitions. Reports themselves can
make use of data from many different data sources.

Using Report Designer


Reporting Services includes two tools for creating reports:

Report Designer can create reports of any complexity that Reporting Services
supports, but requires you to understand the structure of your data and to be
able to navigate the Visual Studio user interface.
Report Builder provides a simpler user interface for creating ad hoc reports,
directed primarily at business users rather than developers. Report Builder
requires a developer or administrator to set up a data model before end users
can create reports.

Well start our tour of Reporting Services with Report Designer. Report Designer
runs inside the Business Intelligence Development Studio shell, and offers several
ways to create reports. You can either use the Report Wizard to quickly create a
report, or you can use a set of design tools to build a report from scratch. You can
also use the design tools to modify a report created with the wizard.

Using the Report Wizard


The easiest way to create a report in Report Designer is to use the Report Wizard.
Like all wizards, the Report Wizard walks you through the process in step-by-step
fashion. You can make the following choices in the wizard:

The data source to use


The query to use to retrieve data
Whether to use a tabular or matrix layout for the report
How to group the retrieved data
What visual style to use
Where to deploy the finished report

Try It!
To create a simple report using the Report Wizard, follow these steps:
1.
2.
3.
4.

Launch Business Intelligence Development Studio.


Select File f New f Project.
Select the Business Intelligence Projects project type.
Select the Report Server Project Wizard template.

Copyright 2005 Accelebrate, Inc

Introduction to SQL Server 2005


18-3

http://www.accelebrate.com

Reporting Services
5. Name the new report ProductReport1 and pick a convenient location to save it
in.
6. Click OK.
7. Read the first page of the Report Wizard and click Next.
8. Name the new data source AdventureWorksDS.
9. Click the Edit button.
10. Log on to your test server.
11. Select the AdventureWorks database.
12. Click OK.
13. Click the Credentials button.
14. Select Use Windows Authentication.
15. Click OK.
16. Check the Make This a Shared Data Source checkbox. This will make this
particular data source available to other Reporting Services applications in the
future.
17. Click Next.
18. Click the Query Builder button.
19. If the full query designer interface does not display by default, click the query
designer toolbar button at the far left end of the toolbar. Figure 18-2 shows the
full query designer interface.

Copyright 2005 Accelebrate, Inc

Introduction to SQL Server 2005


18-4

http://www.accelebrate.com

Using Report Designer

Figure 18-2: Query Builder

20. Click the Add Table toolbar button.


21. Select the Product table and click Add.
22. Click Close.
23. Check the Name, ProductNumber, Color, and ListPrice columns.
24. Click OK.
25. Click Next.
26. Select the Tabular layout and click Next.
27. Move the Color column to the Group area, and the other three columns to the
Detail area, as shown in Figure 18-3.

Copyright 2005 Accelebrate, Inc

Introduction to SQL Server 2005


18-5

http://www.accelebrate.com

Reporting Services

Figure18-3: Grouping columns in the report

28. Click Next.


29. Select the Stepped layout and click Next.
30. Select the Ocean style and click Next.
31. Accept the default deployment location and click Next.
32. Name the report ProductReport1.
33. Check the Preview Report checkbox.
34. Click Finish.
Figure 18-4 shows the finished report, open in Report Designer.

Copyright 2005 Accelebrate, Inc

Introduction to SQL Server 2005


18-6

http://www.accelebrate.com

Using Report Designer

Figure 18-4: Report created by the Report Wizard

Figure 18-4 shows the main features of Report Designer:

The Datasets window shows the data that is available to the report.
The main design window lets you view the report itself. You can see a
preview of the report, work with the report in a layout designer, or work with
the query that returns the data for the report.
The Solution Explorer, Output, and Properties windows are the standard
Visual Studio windows.

Modifying a Report
Now that youve created a report with the Report Wizard, you can modify it with
the Report Designer. If youve used any sort of visual report design tool in the past,
you should have no problem making changes here. Among the possibilities here:

Copyright 2005 Accelebrate, Inc

Introduction to SQL Server 2005


18-7

http://www.accelebrate.com

Reporting Services

You can change the available data or the sort order for the report by
modifying the query on the Data tab.
You can resize or rearrange controls on the Layout tab.
You can use the Properties window to change properties of individual
controls including their font, alignment, colors, and so on.

Try It!
To modify the report that you just created, follow these steps:
1.
2.
3.
4.
5.
6.
7.

8.

9.

Click the Data tab to view the query for the report.
Select a Descending sort type for the ListPrice column.
Click the Layout tab.
Click in the textbox at the top of the report, where the report name is displayed.
Use the Properties window to change the Value property of this control to
Products By Color.
Click on the header for the Product column.
Place the cursor between the Name and Product Number columns to display a
double-headed arrow. Hold down the mouse button and drag the cursor to the
right to widen the Name column.
Place the cursor between the Product Number and ListPrice columns to display
a double-headed arrow. Hold down the mouse button and drag the cursor to
the right to widen the Product Number column.
Click on the Preview tab to view the modified report, as shown in Figure 18-5.

Copyright 2005 Accelebrate, Inc

Introduction to SQL Server 2005


18-8

http://www.accelebrate.com

Using Report Designer

Figure 18-5: Modified product report

Designing a Report From Scratch


You can also use Report Designer to build your own reports starting from scratch. In
general, youll follow these steps to create a report:
1.
2.
3.
4.

Create a Report project in Business Intelligence Design Studio.


Associate one or more datasets with the report.
Create a query from the dataset.
Build the report layout.

Try It!
To create a fresh report in Report Designer, follow these steps:
1.
2.
3.
4.

Select File f Close Solution to close the existing report.


Select File f New f Project.
Select the Business Intelligence Projects project type.
Select the Report Server Project template.

Copyright 2005 Accelebrate, Inc

Introduction to SQL Server 2005


18-9

http://www.accelebrate.com

Reporting Services
5. Name the new report ProductReport2 and pick a convenient location to save it
in.
6. Right-click on the Reports node in Solution Explorer and select Add f New
Item.
7. Select the Report template.
8. Name the new report ProductReport2.rdl and click Add.
9. Select the Data tab in the Report Designer.
10. Click the Datasets dropdown and select <New Dataset> to open the Data
Source dialog box.
11. Name the new Data Source AdventureWorksDS.
12. Click the Edit button.
13. Connect to your test server and choose the AdventureWorks database.
14. Click OK.
15. Click OK again to create the data source.
16. Click the Query Designer toolbar button inside the Data tab to display the full
query designer user interface.
17. Click the Add Table button.
18. Select the Location table.
19. Click Add.
20. Click Close.
21. Check the boxes for the Name and CostRate columns.
22. Sort the dataset in ascending order by Name.
23. Click the Layout tab to show the blank report layout.
24. Hover your mouse over the toolbox tab to display the available controls for the
report.
25. Double-click the List control.
26. Expand the List control to the width of the report.
27. Expand the dataset in the Datasets window to show the column names.
28. Drag the Name and CostRate columns from the Datasets window and drop
them in the List control. Expand them horizontally until each takes up about
half of the List control. Figure 18-6 shows the report in layout view.

Copyright 2005 Accelebrate, Inc

Introduction to SQL Server 2005


18-10

http://www.accelebrate.com

Publishing a Report

Figure 18-6: Designing a report from scratch

29. Select the Preview tab to see the report with data.

Publishing a Report
Creating reports in Business Intelligence Development Studio is good for
developers, but it doesnt help users at all. In order for the reports you build to be
available to others, you must publish them to your Reporting Services server. To
publish a report, you can use the Build and Deploy menu items in Business
Intelligence Development Studio. Before you do this, you need to check the projects
configuration to make sure that youve selected an appropriate server for the
deployment.

Try It!
To publish a report, follow these steps:

Copyright 2005 Accelebrate, Inc

Introduction to SQL Server 2005


18-11

http://www.accelebrate.com

Reporting Services
1.
2.
3.
4.
5.
6.

Close the ProductReport2 project.


Open the ProductReport1 project.
Select Project f ProductReport1 Properties.
Click the Configuration Manager button.
Set the Active Solution Configuration to Production.
Make sure the Configuration is set to Production and both the Build and
Deploy checkboxes are checked. Figure 18-7 shows the Configuration Manager.

Figure 18-7: Setting the active configuration

7. Click Close.
8. Fill in the URL for your Report Server. If youre developing on the same
computer where Reporting Services is installed, and you installed in the default
configuration, this will be http://localhost/ReportServer. Figure 18-8 shows
the completed Property Pages.

Copyright 2005 Accelebrate, Inc

Introduction to SQL Server 2005


18-12

http://www.accelebrate.com

Publishing a Report

Figure 18-8: Setting report project properties

9. Click OK.
10. Select Build f Deploy ProductReport1. The Output Window will track the
progress of BIDS in deploying your report, as shown in Figure 18-9. Depending
on the speed of your computer, building the report may take some time.

Figure 18-9: Deploying a report

11. Launch SQL Server Management Studio.


12. Click the Connect button in Object Explorer and select Reporting Services.
13. Connect to your test server.
14. Expand the Home node in the Object Explorer tree to find the ProductReport1
project.
15. Expand the ProductReport1 project to find the ProductReport1 report.
16. Right-click on the report and select View Report.

Copyright 2005 Accelebrate, Inc

Introduction to SQL Server 2005


18-13

http://www.accelebrate.com

Reporting Services
17. If prompted, supply your username and password. The report will open in
your Web browser.

Using Report Builder


Report Designer gives you one way to create reports for Reporting Services, but its
not the only way. SQL Server 2005 also includes a tool directed at end users named
Report Builder. Unlike Report Designer, which is aimed at Developers, Report
Builder presents a simplified view of the report-building process and is intended for
business analysts and other end users.

Building a Data Model


Report Builder doesnt let end users explore all of a SQL Server database. Instead, it
depends on a data model: a preselected group of tables and relationships that a
developer has identified as suitable for end-user reporting. To build a data model,
you use Business Intelligence Development Studio. Data models contain three
things:

Data Sources connect the data model to actual data.


Data Source Views draw data from data sources.
Report Models contain entities that end users can use on reports.

Try It!
To create a data model, follow these steps:
1.
2.
3.
4.
5.
6.
7.

If its not already open, launch Business Intelligence Development Studio


Select File f New f Project.
Select the Business Intelligence Projects project type.
Select the Report Model Project template.
Name the new project AWSales and save it in a convenient location.
Click OK.
Right-click on Data Sources in Solution Explorer and select Add New Data
Source.
8. Read the first page of the Add New Data Source Wizard and click Next.
9. Click New.
10. In the Connection Manager dialog box connect to the AdventureWorks
database on your test server and click OK.
11. Click Next.
12. Name the new data source AdventureWorks and click Finish.

Copyright 2005 Accelebrate, Inc

Introduction to SQL Server 2005


18-14

http://www.accelebrate.com

Using Report Builder


13. Right-click on Data Source Views in Solution Explorer and select Add New
Data Source View.
14. Read the first page of the Add New Data Source View Wizard and click Next.
15. Select the AdventureWorks data source and click Next.
16. Select the Production.Product table and click the > button to move it to the
Included Objects listbox.
17. Select the Sales.SalesOrderDetail table and click the > button to move it to the
Included Objects listbox.
18. Click the Add Related Tables button.
19. Click Next.
20. Click Finish.
21. Right-click on Report Models in Solution Explorer and select Add New Report
Model.
22. Read the first page of the Report Model Wizard and click Next.
23. Select the Adventure Works data source view and click Next.
24. Keep the default rules selection, as shown in Figure 18-10, and click Next.

Figure 18-10: Creating entities for end-user reporting

Copyright 2005 Accelebrate, Inc

Introduction to SQL Server 2005


18-15

http://www.accelebrate.com

Reporting Services
25. Choose the Update Statistics option and click Next.
26. Click Run to complete the wizard.
27. Click Finish. If you get a warning that a file was modified outside the source
editor, click Yes.
28. Select Build f Deploy AWSales to deploy the report model to the local
Reporting Services server.

Building a Report
Report Builder itself is a ClickOnce Windows Forms application. That means that
end users install it from the browser, but once installed it's not a browser-based
application. To get started with Report Builder, browse to your Reporting Services
home page. Typically, this will have a URL such as
http://ServerName/Reports (or http://localhost/Reports if you're
running the browser on the same box with SQL Server 2005 itself). Figure 18-11
shows the Reporting Services home page.

Figure 18-11: Reporting Services home page

To install Report Builder, click the Report Builder link in the home page menu bar.
Report Builder will automatically load up all of the available report models and wait
for you to choose one to build a report from.

Copyright 2005 Accelebrate, Inc

Introduction to SQL Server 2005


18-16

http://www.accelebrate.com

Using Report Builder

Try It!
1. Open a browser window and navigate to http://localhost/Reports (or
to the appropriate Report Server URL if youre not working on the report
server).
2. Click the Report Builder link.
3. Depending on your operating system, you may have to confirm that you want
to run the application.
4. After Report Builder is installed, select the AdventureWorks report model and
the table report layout. Click OK. Figure 18-12 shows the new blank report that
Report Builder will create.

Figure 18-12: New report in Report Builder

Copyright 2005 Accelebrate, Inc

Introduction to SQL Server 2005


18-17

http://www.accelebrate.com

Reporting Services
The Explorer window to the left of the design surface shows all of
the tables in the report model. Beneath that, the Fields window
shows the entities in the currently-selected table. Note that not
everything in this window is a column in the table: the report
model also contains aggregate entities such as Total Safety Stock
Level and automatically calculated fields.
5. Select the Product table.
6. Drag the Name field and drop it in the area labeled Drag and Drop Column
Fields.
7. Click on Special Offer Products in the Explorer window to show related child
tables.
8. Click on Sales Order Details.
9. Drag the Total Order Qty field and drop it to the right of the Name field.
10. Click where it says Click to Add Title and type Product Sales.
11. Click the Run Report button to produce the report shown in Figure 12-13.

Copyright 2005 Accelebrate, Inc

Introduction to SQL Server 2005


18-18

http://www.accelebrate.com

Using Report Builder

Figure 18-13: Report in Report Builder

12. Click the Sort and Group toolbar button.


13. Select to sort by Total Order Qty descending.
14. Click OK.
15. Select File f Save.
16. Name the new report Product Sales.
17. Click Save. This will publish the report back to the Reporting Services server
that you originally downloaded Report Builder from.

Copyright 2005 Accelebrate, Inc

Introduction to SQL Server 2005


18-19

http://www.accelebrate.com

Reporting Services

Using Report Manager


The Web home page for Reporting Services provides a complete interface for
managing reports (as well as other objects such as data sources and models) after
they are created. This interface, known as Report Manager, is intended primarily for
database administrators, but as a developer you should know about its capabilities
for managing and modifying reports.
When you click on a report in Report Manager, youll see the reports data, as shown
in Figure 18-14.

Figure 18-14: Report in Report Manager

Copyright 2005 Accelebrate, Inc

Introduction to SQL Server 2005


18-20

http://www.accelebrate.com

Using Report Manager


Note that reports in Report Manager open in a tabbed interface. The four tabs allow
you to perform various functions:

View allows you to see the current data in the report.


Properties lets you adjust such things as the reports name, data source,
security credentials, caching, and end-user security.
History shows you saved snapshots of the report.
Subscriptions lets you create subscriptions to the report. Subscriptions allow
you to set up periodic delivery of reports to end users by e-mail or file share.

Copyright 2005 Accelebrate, Inc

Introduction to SQL Server 2005


18-21

http://www.accelebrate.com

Reporting Services

Exercises
Use Report Builder to create a report from the AdventureWorks data model
showing the minimum and maximum order quantity for orders taken by each
salesperson in the company. Youll find the necessary data in the SalesOrderHeader
and SalesOrderDetail tables.

Copyright 2005 Accelebrate, Inc

Introduction to SQL Server 2005


18-22

http://www.accelebrate.com

Solutions to Exercises

Solutions to Exercises
1. Open a browser window and navigate to http://localhost/Reports (or
to the appropriate Report Server URL if youre not working on the report
server).
2. Click the Report Builder link.
3. Select the AdventureWorks report model and the table report layout.
4. Click OK.
5. Select the Sales Order Header table.
6. Drag the Sales Person ID field and drop it in the area labeled Drag and Drop
Column Fields.
7. Click on Sales Order Details in the Explorer window.
8. Expand the Total Order Qty field in the Fields window to show the alternative
fields beneath it.
9. Drag the Min Order Qty field and drop it to the right of the Name field.
10. Drag the Max Order Qty field and drop it to the right of the Min Order Qty
field.
11. Click where it says Click to Add Title and type Sales Performance.
12. Click the Run Report button to produce the report shown in Figure 18-15.

Copyright 2005 Accelebrate, Inc

Introduction to SQL Server 2005


18-23

http://www.accelebrate.com

Reporting Services

Figure 18-15: Sales performance report

Copyright 2005 Accelebrate, Inc

Introduction to SQL Server 2005


18-24

http://www.accelebrate.com

También podría gustarte