E xe rc i se Do c umen t o n Re p o rt i n g S e r vi ce s
SQL SERVER YUKON
SymIndia
EXERCISES ON REPORTING SERVICES
PRE-REQUISITES
Open Business Intelligence Development Studio and create a project by selecting Report Server Server. Create a Data Source with AdventureWorks Database and Authentication In the Project properties set the target path as [Link] Windows NT
I DESIGNING REPORTS EXERCISE1:
LEARNING
POINTS:
Embedded Image Matrix Report Drilldown Grouping and Sorting Use of Text Box Toggle Grouped items on the report
STEPS:
Create a data set with the Query below
Which retrieves Product Category, sub category, Year, Quarter, Month wise sales from ProductSubCategory, SalesOrderHeader, SalesTerritory, SalesOrderDetail tables in AdventureWorks database for period of 2002 and 2003:
E xe rc i se Do c umen t o n Re p o rt i n g S e r vi ce s
SQL SERVER YUKON
SymIndia
SELECT [Link] AS ProdCat, [Link] AS SubCat, DATEPART(yy, [Link]) AS OrderYear, 'Q' + DATENAME(qq, [Link]) AS OrderQtr, DATENAME(mm, [Link]) AS OrderMonth, [Link] as Country, SUM([Link] * [Link]) AS Sales FROM [Link] PS INNER JOIN [Link] SH INNER JOIN [Link] SO ON [Link] = [Link] INNER JOIN [Link] ST ON [Link] = [Link] INNER JOIN [Link] P ON [Link] = [Link] ON [Link] = [Link] INNER JOIN [Link] PC ON [Link] = [Link] WHERE ([Link] BETWEEN '1/1/02' AND '12/31/03') GROUP BY DATEPART(yy, [Link]), [Link], [Link], 'Q' + DATENAME(qq, [Link]), DATENAME(mm, [Link]), [Link], [Link]
Using Toolbox, Add a Matrix to Design Layout Add ProdCat, SubCat fields to row group in the Matrix Add OrderYear, OrderQtr, OrderMonth, Country fields to column group in the Matrix Add sales field to Details group in the matrix For all the rows and columns, Change the Name property appropriately.
GROUPING FIELD INITIAL PROPERTY V ISIBILITY W H E T H E R
AN OTHER CAN BE TOGGED BY
PRODUCT ITEM REPORT ITEM
AND
IF YES, THE
ProductCat Subcat OrderYear
Visible Hidden Visible
ProductCat -
E xe rc i se Do c umen t o n Re p o rt i n g S e r vi ce s
SQL SERVER YUKON
SymIndia
OrderQtr OrderMonth Country
Hidden Hidden Hidden
OrderYear OrderQtr OrderMonth
SORTIN G
SORT the OrderQtr by Ascending.
BACKGROUND
ADD
PROPERTY
Prerequisite task
SUB
PROPERTY
VALUE
drop from
Drag Image Toolbox
and item
Background Image
Source Value
Embedded Chose image Embedded using the
image Wizard Note: image Delete the
control
placed on the report layout, after you set the Background Source and Value
FORMAT
FORMAT
VALUE
E xe rc i se Do c umen t o n Re p o rt i n g S e r vi ce s
SQL SERVER YUKON
SymIndia
Add Border Style & Color to entire matrix Assign background Color for each text box in the matrix Assign Border for each text box in the matrix Drag and drop a text box on top of the matrix report ProductCat SubCat
Solid & Black Yellow
Solid Name of the Report Text Align: Left Text Align : Right
OrderYear, OrderQtr, OrderMonth, Country, Sales
Text Align: Center
Preview the Report
ADDITIONAL SCENARIOS TO TEST:
Sort : quarter, month and country Remove OrderQtr from Matrix Remove OrderMonth Remove the image
EXERCISE 2:
LEARNING POINTS:
Creating Dynamic Queries
STEPS:
E xe rc i se Do c umen t o n Re p o rt i n g S e r vi ce s
SQL SERVER YUKON
SymIndia
Create a Employee dataset with the following query:
Select [Link], [Link], [Link], [Link] From [Link] D INNER JOIN [Link] E ON [Link] = [Link] INNER JOIN [Link] C ON [Link] = [Link] Where [Link]=1 Order By [Link]
Create a Department dataset with the following query:
0 AS DepartmentID, 'All' AS Name DepartmentID, Name [Link] Name
SELECT UNION SELECT FROM ORDER BY
Add a Table in the layout view placing the following fields from the first data set in Detail Area:
Fir stName LastName Title
Using
the
menu
option
Report
->ReportParameters,
create
report
parameter with the following properties set:
Name:Department Data Type: String Prompt: Deparment Allow null value , Allow blank value : False For Available values, select From query. Dataset: Departments Value field: DepartmentID Label field: Name.
E xe rc i se Do c umen t o n Re p o rt i n g S e r vi ce s
SQL SERVER YUKON
SymIndia
Modify the Employee dataset as follows:
="SELECT [Link], [Link], [Link], [Link] " & "From [Link] D " & "INNER JOIN [Link] E " & "ON [Link] = [Link] " & "INNER JOIN [Link] C " & "ON [Link] = [Link] " & Iif(Parameters![Link] = 0, "", Parameters![Link]) & "ORDER BY [Link]" "WHERE [Link] = " &
Preview the report the query will use the report parameter.
EXERCISE 3:
LEARNING POINTS:
Tabular report Grouping Sorting Using Page Header & Page Footer Embedding Image Using page Numbers Interactive Sorting
STEPS:
Create a dataset with the following query:
Which
retrieves
product
wise,
quantity
sold
from
product,
SalesOrderDetail, and SalesOrderheader tables
E xe rc i se Do c umen t o n Re p o rt i n g S e r vi ce s
SQL SERVER YUKON
SymIndia
SELECT [Link], SUM([Link]) AS [ Quantity Sold ], DATEPART(yy, [Link]) AS Year FROM [Link] INNER JOIN [Link] ON [Link] = [Link] INNER JOIN [Link] ON [Link] = [Link] GROUP BY DATEPART(yy, [Link]), [Link]
Add a table to Design Layout. Drag and drop the 3 fields in the Detail Area: Name, year and Quantity Add Itemname field to grouping table properties.
GROUPING FIELD
ItemName (Header)
PROPERTY
Select the following: Include group header Include group footer Page Break at end Repeat group header Repeat group footer
Year (header)
of
Sale Select the following: Include group header Include group footer
ID_Quantity_Sold (Table Footer)
Drag the ID_Quantity_Sold field to the Table Footer area in the same column where ID_Quantity_Sold Column is present. This adds the following expression: =Sum(Fields!ID_Quantity_Sold_.Value)
E xe rc i se Do c umen t o n Re p o rt i n g S e r vi ce s
SQL SERVER YUKON
SymIndia
FORMAT
FORMAT Add Border & Color to entire Table Text Alignment in the Table Data Sorting Assign color & make the font bold for Table Group Header VALUE Solid & Black Center Year Orange
To add Page Numbers in page footers : Navigate menu Report Page Footer. This adds the Footer area in the layout. Drag and drop a text box in the footer and set Expression: =Page : & Globals!PageNumber . (To do this, set
the Value property of the text box).
Right-click the column heading for the year column. Right-click for Properties and select Interactive Sort. Select Add an interactive sort action to this text box. Specify the sort expressions as =Fields![Link].
Preview the report. Check that the effect of GroupHeader is not visible. Check the effect of
Interactive sort.
ADDITIONAL:
Add conditional format Set Expression in Font Property of Quantity Sold Textbox using expression like this:
=iif(Fields!ID_Quantity_Sold_.Value < 300, "Red", "Black")
Add Item Name to table group header and remove it from the Details. This will ensure that Item name is displayed only once as Group Header.
E xe rc i se Do c umen t o n Re p o rt i n g S e r vi ce s
SQL SERVER YUKON
SymIndia
Clear Page Break At End checkbox and drag and drop Quantity field Name Footer (under the Quantity Column) . This will display itemwise totals whenever item changes.
EXERCISE 4:
LEARNING POINTS:
Tabular report Using Multiple data sets Using Parameters Using single and multi- valued parameters
Scenario 1 : Three parameters, all single-valued
Create a dataset named SalesEmployees that uses an AdventureWorks data source that gives EmployeeId and LoginID.
SELECT [Link], [Link] AS Employee FROM [Link] INNER JOIN [Link] ON [Link] = [Link] ORDER BY [Link]
Create
another
dataset
named
EmplSalesMonth
that
uses
an
AdventureWorks data source giving the sales details for a given employee for that month and year.
SELECT [Link] AS Employee, DATEPART(Year, [Link]) AS OrderYear, DATEPART(Month, [Link]) AS OrderMonthNum, DATENAME(Month, [Link]) AS OrderMonth, [Link] AS ProdCat, SUM([Link]) AS Sales FROM [Link] INNER JOIN
E xe rc i se Do c umen t o n Re p o rt i n g S e r vi ce s
SQL SERVER YUKON
SymIndia
[Link] ON [Link] = [Link] INNER JOIN [Link] ON [Link] = [Link] INNER JOIN [Link] ON [Link] = [Link] INNER JOIN [Link] ON [Link] = [Link] INNER JOIN [Link] ON [Link] = [Link] INNER JOIN [Link] ON [Link] = [Link] WHERE (DATEPART (Year, [Link]) in (@ReportYear)) AND (DATEPART(Month, [Link])in( @ReportMonth)) AND ([Link] = @EmpID) GROUP BY [Link], DATEPART(Year, [Link]), DATEPART(Month, [Link]), DATENAME(Month, [Link]), [Link]
Create a third dataset OrderYear to retrieve the years. We will use this for multi-valued parameters.
Select Distinct(Datepart(year,[Link])) as OrderYear from [Link]
Drag and drop the table to the design layout. Right click on the table select tablix properties Dataset name = EmpSales Go to parameters select reportYear right click on it select parameter properties Click on Available values choose get values from a query Set dataset name = OrderYear
10
E xe rc i se Do c umen t o n Re p o rt i n g S e r vi ce s
SQL SERVER YUKON
SymIndia
ValueField = OrderYear labelField = OrderYear Click OK
Again Go to parameters select EmpID right click on it select parameters properties Click on Available values choose get values from a query Set dataset name = SalesEmployee ValueField = EmpID LabelField = EmpID Click ok
Again Go to parameters select ReportMonth right click on it select parameters properties Click on Available values choose specify values from a query Add 12 labels and name them from Jan to Dec, values from 1 to 12 Click Ok
11
E xe rc i se Do c umen t o n Re p o rt i n g S e r vi ce s
SQL SERVER YUKON
SymIndia
Click OK
Run the report for output with the following parameters:-Year =2004 Month = January EmpID = 283.
Scenario 2 : Three parameters, one is multi-valued
Remove the Table from above Report Drag and Drop a list to the design layout Place table into this list. Add data to the table from EmpMONTH dataset Select List and Delete Details from RowGroup (from down left panel of the window)
12
E xe rc i se Do c umen t o n Re p o rt i n g S e r vi ce s
SQL SERVER YUKON
SymIndia
Select List Right Click Add Group Row Group Parent Group Group by = Order Year Click OK Go to parameters Right Click on ReportYear Properties Check Allow multiple vales Click OK
Run the report. Verify that the entire list of unique year values is displayed. Run the report and verify output with the following parameters: Year = 2003, 2004 Month = January EmpID = 283.
Scenario 3 : Three parameters, two are multi-valued
Go to design layout of Above Report Select list select order Year right click add group child group group by = OrderMonth Click OK Go to parameters right click ReportMonth parameters properties check allow multiple values Click default values from left panel Choose specify values add values Value = 1 Value = 2
13
E xe rc i se Do c umen t o n Re p o rt i n g S e r vi ce s
SQL SERVER YUKON
SymIndia
Click OK
Run the report for Output with the following parameters:-Year = 2003, 2004 Month = January, February and March. EmpID = 283.
EXERCISE 5: ORDER DETAILS
LEARNING POINT:
Table report List
STEPS
The following query gets the order details like quantity, price and discount for a given salesordernumber. Create OrderDetails dataset with this following query:
SELECT [Link]. SalesOrderDetailID, [Link], [Link], CASE WHEN [Link] IS NULL THEN 0 ELSE [Link] END AS UnitPriceDiscount, [Link], [Link], [Link], [Link], [Link] FROM [Link] INNER JOIN [Link] ON [Link] = [Link] INNER JOIN [Link] ON [Link] = [Link] WHERE ([Link] = @SalesOrderNumber) ORDER BY [Link]. SalesOrderDetailID
Create an another dataset called SalesOrder that gets the shipping method, shipping address etc for a given salesordernumber:
SELECT [Link], [Link] AS SalesStore, [Link], [Link] AS SalesLoginID, [Link] AS SalesTitle,
14
E xe rc i se Do c umen t o n Re p o rt i n g S e r vi ce s
SQL SERVER YUKON
SymIndia
[Link], [Link] AS ShipMethod, BillAddress.AddressLine1 AS BillAddress1, BillAddress.AddressLine2 AS BillAddress2, [Link] AS BillCity, [Link] AS BillPostalCode, [Link] AS BillStateProvince, ShipAddress.AddressLine1 AS ShipAddress1, ShipAddress.AddressLine2 AS ShipAddress2, [Link] AS ShipCity, [Link] AS ShipPostalCode, [Link] AS ShipStateProvince, [Link] AS ShipCountryRegion FROM [Link] LEFT OUTER JOIN [Link] AS BillAddress INNER JOIN [Link] AS BillStateProvince ON [Link] = [Link] INNER JOIN [Link] AS BillCountryRegion ON [Link] = [Link] ON [Link] = [Link] LEFT OUTER JOIN [Link] AS ShipAddress INNER JOIN [Link] AS ShipStateProvince ON [Link] = [Link] INNER JOIN [Link] AS ShipCountryRegion ON [Link] = [Link] ON [Link] = [Link] LEFT OUTER JOIN [Link] ON [Link] = [Link] LEFT OUTER JOIN [Link] ON [Link] = [Link] LEFT OUTER JOIN [Link] ON [Link] = [Link] WHERE ([Link] = @SalesOrderNumber)
Drag and drop a table on design layout Select table and associate table with OrderDetails dataset Drag and drop a list on above the table and associate it with SalesOrder dataset Place text boxes in the list to retrieve values for: SalesOrderNumber, Current Date (=Now() as value), Order Date and Purchase Order no.
15
E xe rc i se Do c umen t o n Re p o rt i n g S e r vi ce s
SQL SERVER YUKON
SymIndia
Add a group with name as Item and GroupOn Expression as =Fields! [Link]
FORMAT
FIELD
Name, OrderQty, UnitPrice, LinePriceDiscount and Line Total (OrderDetail data set)
DATA ELEMENT
Drag and Drop name to first column in Item group header Drag other Details row Add columns most right column click as you and and drop fields the in
GROUPING PROPERTIES
proceed (Select right ->Insert
Column to the right) LineTotal Table footer (in the last column)
While previewing the report, you need to provide the value for SalesOrderNumber explicitly (e.g SO43860). This restriction will be removed in the next exercise.
EXERCISE 6:
LEARNING POINTS:
Tabular report Grouping Using Page Header & Page Footer
16
E xe rc i se Do c umen t o n Re p o rt i n g S e r vi ce s
SQL SERVER YUKON
SymIndia
Drill thru Report Text Decoration Currency Format
STEPS
Use Report Generation Wizard with the inputs as provided below: Create a dataset with the following query
Which retrieves TerritoryName, SalesPersonId, Name, Salesordernumber, TotalSales from SalesTerritory, SalesPerson, Employee, SalesOrderHeader tables in the AdventureWorks database):
SELECT [Link], [Link], [Link], [Link], [Link] FROM [Link] INNER JOIN [Link] ON [Link] = [Link] INNER JOIN [Link] ON [Link] = [Link] INNER JOIN [Link] ON [Link] = [Link] AND [Link] = [Link] GROUP BY [Link], [Link], [Link], [Link], [Link] ORDER BY [Link]
Report Type as Tabular Name, LoginId and SalesOrderNo fields as groups TotalDue as Detail field
17
E xe rc i se Do c umen t o n Re p o rt i n g S e r vi ce s
SQL SERVER YUKON
SymIndia
Once the report is created, for the SalesOrderNo. Text box, set the Navigation->HyperLinkAction as Jump To Report. Specify the report made in the previous exercise. Provide Parameter name and value. ( SalesOrderNumber, =Fields![Link])
18
E xe rc i se Do c umen t o n Re p o rt i n g S e r vi ce s
SQL SERVER YUKON
SymIndia
Set Font->Decoration for SalesOrderNo. As Underline
EXERCISE 7: PRODUCT CATALOG
LEARNING POINTS:
Data bound images Document Map Sorting Page breaks Nested Data Regions
STEPS
Create a dataset with that lists the product category, subcategory, name price and photo of products:
19
E xe rc i se Do c umen t o n Re p o rt i n g S e r vi ce s
SQL SERVER YUKON
SymIndia
SELECT [Link], [Link], [Link], [Link] AS Category, [Link] AS SubCategory FROM [Link] INNER JOIN [Link] ON [Link] = [Link] INNER JOIN [Link] ON [Link] = [Link] INNER JOIN [Link] ON [Link] = [Link] INNER JOIN [Link] ON [Link] = [Link] GROUP BY [Link], [Link], [Link], [Link], [Link]
Drag and drop a list on the layout area and drag and drop a text box on the list.
For the text box, set value property as =Fields![Link] Set DataSet property for the list In List properties, click the button Edit DetailsGrouping , Create a group on the list. Value is =Fields![Link]
Drag and drop another list within the first list. Place a text box within this list.
For the text box in the inner list, set value property as =Fields! [Link]
For
the
inner
list,
in a
List
properties, on the
click list.
the Value
button is
Edit
DetailsGrouping. [Link]
Create
group
=Fields!
Drag and drop another list (third) within the second and place a table in this list.
Create a group for the third list with value as =Fields![Link]
20
E xe rc i se Do c umen t o n Re p o rt i n g S e r vi ce s
SQL SERVER YUKON
SymIndia
In the Details row in the table, Drag and drop the two fields: Name and ListPirce.
Drag and drop an Image on the third column, This opens up the Image Wizard. Provide the following details: ImageSource as Database, Dataset as the one you have created already, ThumbNailPhoto as the Image Field.
For all the three lists, in the properties, select Navigation tab. Set the
Document Map
Value.
Label property
to be the same as the Group Detail
For the outermost list, check the checkbox titled Insert a Page Break before the list.
EXERCISE 8: SALES VS. BUDGET REPORT
LEARNING POINTS:
Charts Grouping Sorting
Steps:
Create a dataset with the following query
Which retrieves Region wise, Category wise, Sub category wise products sales and budget
SELECT [Link] AS REGION, [Link] AS CATEGORY, [Link] AS SUBCATEGORY, SUM([Link] * [Link]) AS SALES, SUM([Link] * [Link]) AS BUDGET FROM [Link] PS INNER JOIN [Link] SH INNER JOIN [Link] SO ON [Link] = [Link] INNER JOIN [Link] ST ON [Link] = [Link] INNER JOIN [Link] P ON [Link] = [Link] ON
21
E xe rc i se Do c umen t o n Re p o rt i n g S e r vi ce s
SQL SERVER YUKON
SymIndia
INNER JOIN
[Link] = [Link]
[Link] PC ON [Link] = [Link] GROUP BY [Link], [Link], [Link]
Place a list on the layout. Set its DataSet property. Create a DetailsGroup with value as =Fields![Link] Set the CheckBox Insert a page break after this list. Add a table within the list Add two groups for the table: Category and SubCategory Drag and drop Category field in the category group header (first column) Drag and drop SubCategory field in the SubCategory group header (second column) Place Sales and Budget fields in the Details area Within the list , drag and drop a Chart on the page before the table. For the chart, set properties as follows:
Data Pane: Set the DataSetName Add three CategoryGroups :=Fields![Link],Fields![Link], Fields![Link]
o o
Values: =Sum(Fields![Link]) , For the table properties check the checkbox of the following properties: Insert Page break after the table, Repeat header rows and row footers
22
E xe rc i se Do c umen t o n Re p o rt i n g S e r vi ce s
SQL SERVER YUKON
SymIndia
II MANAGING REPORTS EXERCISE:
LEARNING
POINTS:
Standard subscr iptio n Data Driven subscript ion
Getting Started
Ensure that SQL Server agent service is running Create four shared folders in your system (This is required to test datadriven and standard subscriptions using FileShareDelivery Technique). Build and Deploy the project created earlier.
Standard subscription
Go to the following URL of Report Manager:
[Link] ocalhost/reports
Select the Matrix report (done in Exercise 1) thru the home page of the Report Manager
In Properties pane ->DataSources: Select the radio button A Custom Data Source Connect ion Type: Microsoft SQL Server connect ion string: Datasource= localhost;init ialcatalog=adventureworks Connect ion Using : credentials stored securely in the report server Specify Username & password1 (Local computer user).
23
E xe rc i se Do c umen t o n Re p o rt i n g S e r vi ce s
SQL SERVER YUKON
SymIndia
Select Use as Windows Credentials when connecting to the data source
In Subscript ions pane: Select New subscript ion o o o Select Report Delivery Option ->Report Server Fileshare. Give appropr iate filename. Select the path of one of the shared folders you created (Path to be given in UNC eg:- \\computername\Drive$\sharename). o o o o o Select the render format of the report. Give credentials to access the file share (user ID and Password). Select Overwrite file share option. Select the schedule to run the report Schedule: hour, Give the appropr iate time to run the report and give appropr iate start time. o After the start time, View the shared directory and check that the report has been dropped.
Data Driven subscription
Create a Subscriberdetail table in adventureworks database as follows:
i f ex i st s ( se l e c t * f ro m d b o . s y s o b j e c t s wh e r e i d = o b j ec t _ i d( N' [ d b o ] . [ su b s c r i b e r d e t a i l ] ' ) a n d O B J E C T P R O P E R T Y ( i d , N' I s U s e r T a b l e' ) = 1) d ro p t ab l e [ db o ] . [ s u b s c r i b e r d e t a i l ] GO C R E A T E TA B L E [ db o ] . [ s u b s c r i b e r d e t a i l ] ( [ Na m e ] [ v a r c h a r ] ( 3 0) C O L L A T E S Q L_ L a t i n 1 _ G e n e r a l _ C P 1 _ C I _ A S NO T NU L L , [ F i l e _ S h a r e ] [ v a r c h a r ] ( 1 65) CO L L A T E S QL _ L a t i n 1 _ G e n e r a l _ C P1 _ C I _ A S NO T NU L L ,
24
E xe rc i se Do c umen t o n Re p o rt i n g S e r vi ce s
SQL SERVER YUKON
SymIndia
[ E m p l o y e e I D ] [ v a r c h a r ] ( 5 0) CO L L A T E S QL _ L a t i n 1 _ G e n e r a l _ C P1 _ C I _ A S NU L L , [ Me di a _ f o r m a t ] [ v a r c h a r ] ( 5 0) CO L L A T E S QL _ L a t i n 1 _ G e n e r a l _ C P1 _ C I _ A S NU L L ) O N [PR I MA R Y ] GO
Insert Rows into the above table
I n s e r t i n to [ db o ] . [ s u b s c r i b e r d e t a i l ] v al u e s( l o c a l h o s t \ u s e r , <F i l e sh a r e 1 >, 1 0, C S V ) I n s e r t i n to [ db o ] . [ s u b s c r i b e r d e t a i l ] v al u e s( A r u n , <F i l e sh a r e 2> , 1 0, E x c el ) I n s e r t i n to [ db o ] . [ s u b s c r i b e r d e t a i l ] v al u e s( M ee n a , < F i l e s h a r e 3 >, 1 0, HT ML 4. 0 )
Go to the following URL of Report Manager:
[Link] ocalhost/reports
Select the Matrix report thru the home page of the Report Manager
In Subscript ion pane, Select New Data driven subscript ion o Delivery Option o Descriptio n: Name of the subscr iptio n. Specify how recipie nts are notif ied: Report Server File share
Connect to the subscriberdetail table connect ion string: Datasource localhost; init ialcatalog=adventureworks Credentials (localhost\user, password) Use Windows Credentials
25
E xe rc i se Do c umen t o n Re p o rt i n g S e r vi ce s
SQL SERVER YUKON
SymIndia
Query the subscr iberdetail table as follows and validate query:
S el ec t * f ro m s u b sc r i b e r d e t a i l
o Choose the delivery extension settings o o Filename: appropr iate File extension: true Path: get value from database->file_share Render format: get value from database->Media_format Username: (localhost\user, password) WriteMode: Overwrite
Select the schedule to run the report Schedule: hour, Give the appropr iate time to run the report and give appropr iate start time.
After the start time check the file shares for each subscriber.
II URL ACCESS EXERCISE:
LEARNING
POINTS:
URL Access To Report Server
SCENARIOS
Use Command Parameter ListChildren, Render Render by specifying Format property Pass a parameter and render a report that is parameterized
26
E xe rc i se Do c umen t o n Re p o rt i n g S e r vi ce s
SQL SERVER YUKON
SymIndia
III PROGRAMMATIC ACCESS TO REPORTING WEB SERVICE EXERCISE:
LEARNING
POINTS:
URL Access To Report Server Perf orming Management activities Programmatic Generatio n of RDL
SCENARIOS
Create a WinForms application and add reference to Report Server Web Services ([Link] [Link]
Management Related
List all items in a given path (ListChildren method) List all properties of a given item folder or report item (GetProperties method) Create a new Folder (CreateFolder method) Create a Subscription (CreateSubscription method)
Executing a report
[Link] rs = new [Link](); [Link] = [Link]; [Link] = "[Link] // Render arguments byte[] result = null;
27
E xe rc i se Do c umen t o n Re p o rt i n g S e r vi ce s
SQL SERVER YUKON
SymIndia
string reportPath = "/<Your Report Project>/Report2"; string format = "MHTML"; string historyID = null; string devInfo = @"<DeviceInfo><Toolbar>False</Toolbar></DeviceInfo>"; [Link][] credentials = null; string showHideToggle = null; string encoding; string mimeType; [Link][] warnings = null; [Link][] reportHistoryParameters = null; string[] streamIDs = null; ExecutionInfo execInfo = new ExecutionInfo(); ExecutionHeader execHeader = new ExecutionHeader(); [Link] = execHeader; execInfo = [Link](reportPath, historyID); String SessionId = [Link]; [Link]("SessionID: {0}", [Link]); try { result = [Link](format, devInfo, out encoding, out mimeType, out warnings, out streamIDs); execInfo = [Link](); [Link]("Execution date and time: {0}", [Link]()); } catch (SoapException ex) { [Link]([Link]); } // Write the contents of the report to an MHTML file. try { FileStream stream = [Link]("[Link]", [Link]); [Link]("File created."); [Link](result, 0, [Link]); [Link]("Result written to the file."); [Link](); } catch (Exception ex) { [Link]([Link]); }
28
E xe rc i se Do c umen t o n Re p o rt i n g S e r vi ce s
SQL SERVER YUKON
SymIndia
IV PROGRAMMATIC GENERATION OF REPORT FILE
LEARNING
POINTS:
Usi ng API to create a report def init ion programmaticall y
Copy the following structure of the RDL Code:
u si n g S y s t e m ; u si n g S y s t e m . C o l l e c t i o n s ; u si n g S y s t e m . D a t a ; u si n g S y s t e m . D a t a . S q l C l i e n t ; u si n g S y s t e m . I O ; u si n g S y s t e m . T e x t ; u si n g S y s t e m . X m l ; n a m e s p a c e S a mp l e R D L G e n e r a t o r { c l a s s R dl G e n e r a t o r { SqlConnection m_connection; s tr i n g m_ c o n n e c tS t r i n g; s tr i n g m_ c o m m a n d T e x t ; A rr ay L i s t m_ f i el d s ; p u b l i c s ta t i c v o i d Ma i n ( ) {
29
E xe rc i se Do c umen t o n Re p o rt i n g S e r vi ce s
SQL SERVER YUKON
SymIndia
Rd l G e n e r a t o r m y R dl G e n e r a t o r = n e w R dl G e n e r a t o r ( ) ; my Rd l G e n e r a t o r . R u n ( ) ; } p u b l i c v o i d Ru n ( ) { try { / / Ca l l me t ho d s to c r e at e t h e RD L t hi s . O p e n C o n n e c t i o n ( ) ; t hi s . G e n e r a t e F i e l d s L i s t ( ) ; t hi s . G e n e r a t e R d l ( ) ; C o n so l e . W r i t e L i n e( " R D L f i l e g en e r a t e d su c c e s s f u l l y . " ) ; } c a tc h ( E x c ep ti o n ex c e p t i o n ) { C o n so l e . W r i t e L i n e( " A n er r o r o c c u r r e d: " + ex c ep t i o n . M e s s a g e ) ; } f i n al l y { / / Cl o s e th e c o n n e c t i o n s t r i n g m_connection.Close(); } }
30
E xe rc i se Do c umen t o n Re p o rt i n g S e r vi ce s
SQL SERVER YUKON
SymIndia
p u b l i c v o i d Op e n C o n n e c t i o n ( ) { / / T O D O : Op e n a c o n n ec t i o n t o t h e sa m p l e da t ab a s e } p u b l i c v o i d Ge n e r a t e F i e l d s L i s t ( ) { / / T O D O : Ge n e r a t e a l i s t o f f i el d s f o r a r e p o r t q u e r y } p u b l i c v o i d Ge n e r a t e R d l ( ) { / / T O D O : Ge n e r a t e RD L u si n g X ml T e x t W r i t e r } } }
Create Connection to the AdventureWorks Database as follows:
Replace the Open connection method by the code below
p u b l i c v o i d Op e n C o n n e c ti o n ( ) { / / C re a t e a c o n n e c ti o n o b j ec t m_ c o n n e c ti o n = n e w S q l Co n n e c t i o n ( ) ;
31
E xe rc i se Do c umen t o n Re p o rt i n g S e r vi ce s
SQL SERVER YUKON
SymIndia
/ / C re a t e t he c o n n ec t i o n s t r i n g m_ c o n n e c tS t r i n g = " da t a so u r c e = l o c a l h o s t ; i n i t i a l c at a l o g = A d v e n t u r e Wo r k s ; i n t e g r a t e d s ec u r i t y = S S P I " ; m_ c o n n e c ti o n . C o n n e c t i o n S t r i n g = m_ c o n n e c t S t r i n g ; / / O p e n th e c o n n e c ti o n m_ c o n n e c ti o n . O p e n ( ) ; }
Generate the filed list using a query as follows:
Use Array List for storing the fields generated thru the query as below:
p u b l i c v o i d Ge n e r a t e F i e l d s L i s t ( ) { S q l Co m m a n d c o m m a n d ; S q l D a t a R e a d e r re a d e r; / / E x e c u t i n g a q u e ry to r et r i e v e a f i el d s l i st f o r t h e r ep o r t c o m m a n d = m _ c o n n e c ti o n . C r e a t e C o m m a n d ( ) ; m_ c o m m a n d T e x t = " S E L E C T Pe r s o n . C o u n t r y R e g i o n . Na m e A S Co u n t r y Na m e , Pe r s o n . S t a t e Pr o v i n c e . N a m e A S S t at e P r o v i n c e " + " F R O M Pe r s o n . S t a t e Pr o v i n c e " + " I NNE R J O I N Pe r s o n . C o u n t r y R e g i o n O N Pe r s o n . S t a t e Pr o v i n c e . C o u n t r y R e g i o n C o d e = Pe r s o n . C o u n t r y R e gi o n . C o u n t r y R e g i o n C o d e " + " O R D E R B Y Pe r s o n . C o u n t r y R e g i o n . N a m e " ;
32
E xe rc i se Do c umen t o n Re p o rt i n g S e r vi ce s
SQL SERVER YUKON
SymIndia
c o m m a n d . C o m m a n d T e x t = m_ c o m m a n d T e x t ;
/ / E x e c u t e an d c r ea t e a r ea d e r f o r t h e c u r r e n t c o m m a n d re a d e r = c o m m a n d . E x e c u t e R e a d e r ( C o m m a n d B e h a v i o r . S c h e m a O n l y ) ;
/ / F o r e ac h f i el d i n t he r es u l t s e t , a dd t h e n a m e t o a n a r r a y l i s t m_ f i el d s = n e w A rr ay L i s t( ) ; f o r ( i n t i = 0; i < = r ea d e r . F i e l d C o u n t - 1; i + + ) { m _ f i e l d s . A d d ( r e a d e r . G e t Na m e ( i ) ) ; } }
Use the XMLTextWriter class to generate the raw RDL File. Use the connection string and the field list generated in open Connection method and the Generate fields method as follows:
p u b l i c v o i d Ge n e r a t e R d l ( ) { / / O p e n a n e w RD L f i l e s tr e a m f o r wr i t i n g F i l e S t r e a m s tr e a m ; st r e a m = F i l e . O p e n W r i t e ( " R e p o r t 1 . r d l " ) ; Xm l T ex t W r i t e r wr i t e r = n e w Xm l T e x t W r i t e r ( s t r e a m , E n c o d i n g . U T F 8 ) ; / / C au s e s c hi l d el e m e n t s to b e i n de n t e d
33
E xe rc i se Do c umen t o n Re p o rt i n g S e r vi ce s
SQL SERVER YUKON
SymIndia
wr i t e r . F o r m a t t i n g = F o r m a t t i n g . I n d e n t e d ; / / R ep o r t e l e m e n t wr i t e r . W r i t e Pr o c e s s i n g I n s t r u c t i o n ( " x m l " , "v er s i o n = \ " 1 . 0\ " e n c o d i n g = \ " u tf - 8\ "" ) ; wr i t e r . W r i t e S t a r t E l e m e n t ( " R e p o r t " ) ; wr i t e r . W r i t e A t t r i b u t e S t ri n g ( " x m l n s " , n u l l , " ht tp : / / s c h e m a s . m i c r o s o f t . c o m / s q l s e r v e r / r e p o r t i n g / 2 0 03 / 1 0/ r e p o r t d e f i n i ti o n " ) ; wr i t e r . W r i t e E l e m e n t S t r i n g ( " Wi d t h " , "6i n ") ; / / D at a S o u r c e el e m e n t wr i t e r . W r i t e S t a r t E l e m e n t ( " D a t a S o u r c e s " ) ; wr i t e r . W r i t e S t a r t E l e m e n t ( " D a t a S o u r c e ") ; wr i t e r . W r i t e A t t r i b u t e S t ri n g ( " N a m e " , n u l l , " D at a S o u r c e 1 " ) ; wr i t e r . W r i t e S t a r t E l e m e n t ( " C o n n e c t i o n P r o p e r t i e s " ) ; wr i t e r . W r i t e E l e m e n t S t r i n g ( " D a t a P r o v i d e r " , " S Q L " ) ; wr i t e r . W r i t e E l e m e n t S t r i n g ( " C o n n e c t S t r i n g " , m _ c o n n e c t S t r i n g ) ; wr i t e r . W r i t e E l e m e n t S t r i n g ( " I n t e g r a t e d S e c u r i t y " , "t r u e ") ; wr i t e r . W r i t e E n d E l e m e n t ( ) ; / / C o n n e c t i o n P r o p e r t i e s wr i t e r . W r i t e E n d E l e m e n t ( ) ; / / D at a S o u r c e wr i t e r . W r i t e E n d E l e m e n t ( ) ; / / D at a S o u r c e s / / D at a S e t el e m e n t wr i t e r . W r i t e S t a r t E l e m e n t ( " D a t a S e t s " ) ; wr i t e r . W r i t e S t a r t E l e m e n t ( " D a t a S e t " ) ; wr i t e r . W r i t e A t t r i b u t e S t ri n g ( " N a m e " , n u l l , " D at a S e t 1 " ) ;
34
E xe rc i se Do c umen t o n Re p o rt i n g S e r vi ce s
SQL SERVER YUKON
SymIndia
/ / Q u e ry el e m e n t wr i t e r . W r i t e S t a r t E l e m e n t ( " Q u e r y " ) ; wr i t e r . W r i t e E l e m e n t S t r i n g ( " D a t a S o u r c e Na m e " , " Da t a S o u r c e 1 " ) ; wr i t e r . W r i t e E l e m e n t S t r i n g ( " C o m m a n d T y p e " , "T ex t ") ; wr i t e r . W r i t e E l e m e n t S t r i n g ( " C o m m a n d T e x t " , m _ c o m m a n d T e x t ) ; wr i t e r . W r i t e E l e m e n t S t r i n g ( " T i m e o u t " , " 3 0" ) ; wr i t e r . W r i t e E n d E l e m e n t ( ) ; / / Q u e r y / / F i el d s e l e m e n t s wr i t e r . W r i t e S t a r t E l e m e n t ( " F i e l d s " ) ; f o r e ac h ( s tr i n g f i e l d Na m e i n m_ f i el d s ) { wri t e r . W r i t e S t a r t E l e m e n t ( " F i el d " ) ; wri t e r . W r i t e A t t ri b u t e S t r i n g ( " Na m e " , n u l l , f i e l d Na m e ) ; wri t e r . W r i t e E l e m e n t S t r i n g ( " D a t a F i e l d " , n u l l , f i e l d Na m e ) ; wri t e r . W r i t e E n d E l e m e n t ( ) ; / / F i e l d } / / E n d p r ev i o u s e l e m e n t s wr i t e r . W r i t e E n d E l e m e n t ( ) ; / / F i el d s wr i t e r . W r i t e E n d E l e m e n t ( ) ; / / D at a S e t wr i t e r . W r i t e E n d E l e m e n t ( ) ; / / D at a S e t s / / B o dy e l e m e n t wr i t e r . W r i t e S t a r t E l e m e n t ( " B o d y ") ; wr i t e r . W r i t e E l e m e n t S t r i n g ( " He i g h t " , " 5i n " ) ;
35
E xe rc i se Do c umen t o n Re p o rt i n g S e r vi ce s
SQL SERVER YUKON
SymIndia
/ / R ep o r t I t e m s el e m e n t wr i t e r . W r i t e S t a r t E l e m e n t ( " R e p o r t I t e m s " ) ; / / T ab l e el e m e n t wr i t e r . W r i t e S t a r t E l e m e n t ( " T a b l e " ) ; wr i t e r . W r i t e A t t r i b u t e S t ri n g ( " N a m e " , n u l l , " T a b l e 1 " ) ; wr i t e r . W r i t e E l e m e n t S t r i n g ( " D a t a S e t N a m e " , " D a t a S e t 1 " ) ; wr i t e r . W r i t e E l e m e n t S t r i n g ( " T o p " , " .5 i n ") ; wr i t e r . W r i t e E l e m e n t S t r i n g ( " L e f t " , ". 5i n ") ; wr i t e r . W r i t e E l e m e n t S t r i n g ( " He i g h t " , " .5 i n ") ; wr i t e r . W r i t e E l e m e n t S t r i n g ( " Wi d t h " , ( m _ f i e l d s . C o u n t * 1. 5) + "i n ") ; / / T ab l e Co l u m n s wr i t e r . W r i t e S t a r t E l e m e n t ( " T a b l e C o l u m n s " ) ; f o r ( i n t i = 0; i < m _ f i e l d s . C o u n t ; i + + ) { wri t e r . W r i t e S t a r t E l e m e n t ( " T a b l e C o l u m n " ) ; wri t e r . W r i t e E l e m e n t S t r i n g ( " W i d t h " , " 1 . 5i n ") ; wri t e r . W r i t e E n d E l e m e n t ( ) ; / / T a b l e C o l u m n } wr i t e r . W r i t e E n d E l e m e n t ( ) ; / / T ab l e C o l u m n s / / He a d e r Ro w wr i t e r . W r i t e S t a r t E l e m e n t ( " He a d e r " ) ; wr i t e r . W r i t e S t a r t E l e m e n t ( " T a b l e R o w s " ) ; wr i t e r . W r i t e S t a r t E l e m e n t ( " T a b l e R o w" ) ;
36
E xe rc i se Do c umen t o n Re p o rt i n g S e r vi ce s
SQL SERVER YUKON
SymIndia
wr i t e r . W r i t e E l e m e n t S t r i n g ( " He i g h t " , " . 2 5i n " ) ; wr i t e r . W r i t e S t a r t E l e m e n t ( " T a b l e C e l l s " ) ; f o r e ac h ( s tr i n g f i e l d Na m e i n m_ f i el d s ) { wri t e r . W r i t e S t a r t E l e m e n t ( " T a b l e C e l l " ) ; wri t e r . W r i t e S t a r t E l e m e n t ( " R e p o r t I t e m s " ) ; / / Te x t b o x wri t e r . W r i t e S t a r t E l e m e n t ( " T e x t b o x ") ; wri t e r . W r i t e A t t ri b u t e S t r i n g ( " Na m e " , n u l l , " He a d e r " + f i e l d Na m e ) ; wri t e r . W r i t e S t a r t E l e m e n t ( " S t y l e " ) ; wri t e r . W r i t e E l e m e n t S t r i n g ( " T e x t D e c o r a t i o n " , " Un d e r l i n e " ) ; wri t e r . W r i t e E n d E l e m e n t ( ) ; / / S t y l e wri t e r . W r i t e E l e m e n t S t r i n g ( " T o p ", " 0i n ") ; wri t e r . W r i t e E l e m e n t S t r i n g ( " L e f t " , " 0i n ") ; wri t e r . W r i t e E l e m e n t S t r i n g ( " He i g h t " , ". 5i n " ) ; wri t e r . W r i t e E l e m e n t S t r i n g ( " W i d t h " , " 1 . 5i n ") ; wri t e r . W r i t e E l e m e n t S t r i n g ( " V a l u e " , f i e l d Na m e ) ; wri t e r . W r i t e E n d E l e m e n t ( ) ; / / T e x t b o x wri t e r . W r i t e E n d E l e m e n t ( ) ; / / Re p o r t I t e m s wri t e r . W r i t e E n d E l e m e n t ( ) ; / / T a b l e C e l l } wr i t e r . W r i t e E n d E l e m e n t ( ) ; / / T ab l e C e l l s wr i t e r . W r i t e E n d E l e m e n t ( ) ; / / T ab l e R o w
37
E xe rc i se Do c umen t o n Re p o rt i n g S e r vi ce s
SQL SERVER YUKON
SymIndia
wr i t e r . W r i t e E n d E l e m e n t ( ) ; / / T ab l e R o ws wr i t e r . W r i t e E n d E l e m e n t ( ) ; / / He a d e r / / D et a i l s Ro w wr i t e r . W r i t e S t a r t E l e m e n t ( " D e t a i l s " ) ; wr i t e r . W r i t e S t a r t E l e m e n t ( " T a b l e R o w s " ) ; wr i t e r . W r i t e S t a r t E l e m e n t ( " T a b l e R o w" ) ; wr i t e r . W r i t e E l e m e n t S t r i n g ( " He i g h t " , " . 2 5i n " ) ; wr i t e r . W r i t e S t a r t E l e m e n t ( " T a b l e C e l l s " ) ; f o r e ac h ( s tr i n g f i e l d Na m e i n m_ f i el d s ) { wri t e r . W r i t e S t a r t E l e m e n t ( " T a b l e C e l l " ) ; wri t e r . W r i t e S t a r t E l e m e n t ( " R e p o r t I t e m s " ) ; / / Te x t b o x wri t e r . W r i t e S t a r t E l e m e n t ( " T e x t b o x ") ; wri t e r . W r i t e A t t ri b u t e S t r i n g ( " Na m e " , n u l l , f i e l d Na m e ) ; wri t e r . W r i t e S t a r t E l e m e n t ( " S t y l e " ) ; wri t e r . W r i t e E n d E l e m e n t ( ) ; / / S t y l e wri t e r . W r i t e E l e m e n t S t r i n g ( " T o p ", " 0i n ") ; wri t e r . W r i t e E l e m e n t S t r i n g ( " L e f t " , " 0i n ") ; wri t e r . W r i t e E l e m e n t S t r i n g ( " He i g h t " , ". 5i n " ) ; wri t e r . W r i t e E l e m e n t S t r i n g ( " W i d t h " , " 1 . 5i n ") ; wri t e r . W r i t e E l e m e n t S t r i n g ( " V a l u e " , "= F i el d s ! " + f i el d Na m e + ".Value");
38
E xe rc i se Do c umen t o n Re p o rt i n g S e r vi ce s
SQL SERVER YUKON
SymIndia
wri t e r . W r i t e E l e m e n t S t r i n g ( " Hi d e D u p l i c a t e s " , " D at a S e t 1 " ) ; wri t e r . W r i t e E n d E l e m e n t ( ) ; / / T e x t b o x wri t e r . W r i t e E n d E l e m e n t ( ) ; / / Re p o r t I t e m s wri t e r . W r i t e E n d E l e m e n t ( ) ; / / T a b l e C e l l } / / E n d De t ai l s el e m e n t a n d c h i l dr e n wr i t e r . W r i t e E n d E l e m e n t ( ) ; / / T ab l e C e l l s wr i t e r . W r i t e E n d E l e m e n t ( ) ; / / T ab l e R o w wr i t e r . W r i t e E n d E l e m e n t ( ) ; / / T ab l e R o ws wr i t e r . W r i t e E n d E l e m e n t ( ) ; / / D et a i l s
/ / E n d ta b l e el e m e n t an d en d r ep o r t d ef i n i t i o n f i l e wr i t e r . W r i t e E n d E l e m e n t ( ) ; / / T ab l e wr i t e r . W r i t e E n d E l e m e n t ( ) ; / / R ep o r t I t e m s wr i t e r . W r i t e E n d E l e m e n t ( ) ; / / B o dy wr i t e r . W r i t e E n d E l e m e n t ( ) ; / / R ep o r t / / F l u s h t he wr i t e r a n d c l o s e t h e st r e a m wr i t e r . F l u s h ( ) ; st r e a m . C l o s e ( ) ; }
Build the project. View the report generated by the RDL file in the bin directory.
39
E xe rc i se Do c umen t o n Re p o rt i n g S e r vi ce s
SQL SERVER YUKON
SymIndia
40
E xe rc i se Do c umen t o n Re p o rt i n g S e r vi ce s
SQL SERVER YUKON
SymIndia
V CREATING AND USING A REPORT MODEL
LEARNING
POINTS:
Creating a Report Model Usi ng Report Buil der
STEPS
Create a Project of type Report Model using Visual Studio 2005. Create a new Data Source using a new connection to Adventure works. Create a Data Source View with the following tables:
[Link] Person. Contact [Link] [Link] [Link] [Link] [Link] [Link] [Link]
41
E xe rc i se Do c umen t o n Re p o rt i n g S e r vi ce s
SQL SERVER YUKON
SymIndia
[Link] [Link] [Link] [Link] [Link] [Link]
Create a new Report Model.
Leave all defaults in the Report Model wizard. Run the Report Model wizard.
Build and deploy the project. Open the webpage [Link] . Select the Models-> Report builder. In the Report Builder, Create a new report :
Add Title as Sales Territory Report. Drop the Sales Territory Entity from the Explorers list into the report layout area labeled Drop Column Field. In the Explorers list select Sales People as Territory -> Sales Person->SalesPerson->Contact.
42
E xe rc i se Do c umen t o n Re p o rt i n g S e r vi ce s
SQL SERVER YUKON
SymIndia
From the Fields list, select First Name & Last Name and drop it to the right of Sales Territory column in the report layout. In the Explorers list, Select Sales Person->Sales Order Headers From the Fields list, drop the #Sales Order Headers field to the right of Last name column in the report layout. From the Fields list, drop the Sum Total Due to the right of
#SalesOrderHeader in the Report Layout. In the toolbar, Select Filter. In the Explorer list, select the Sales Person->Sales Order header Entity. From the Fields list, in the text boxes: OrderYear = 2002 OrderMonth = 3 expand Order Date. Drop Order Year and
Order Month fields into the data area and specify the filter value
Save the Report definition. Run the report in the Report Builder and test the report. Switch to Design Report select row below Sum Total due column header, right click -> format the and change the following: Number-> defined formats ->$ 1,234.56.
Run the Report.
43
E xe rc i se Do c umen t o n Re p o rt i n g S e r vi ce s
SQL SERVER YUKON
SymIndia
* * * E n d of D o cum e n t * * *
44