Microsoft SQL Reporting Services – Running a Report from the Command Line

Author:  Ajit Mhaiskar
Published: 1/6/2005

 

Introduction

I recently ran into a need to run a report in SQL reporting services from the command line. The Report took four (4) input parameters and I had to export it to Microsoft® Excel and save it to disk. I had to rummage through the product documentation and the Microsoft® SQL Reporting newsgroup to get this right. For running reports from the command line, SQL Reporting services provide a utility called “rs utility”.

The rs Utility

The rs utility is a script host that processes script you provide in an input file. You can define scripts to administer a report server, copy report server database content to another database, publish reports, and so forth. The script must be written in Microsoft Visual Basic® .NET code, and stored in a Unicode or UTF-8 text file with a .rss file extension. You cannot debug scripts with the rs utility. To debug a script, run the code from within Visual Studio.

To run the tool, you must be a local administrator on the computer that has the report server instance you are running the script against. There are some script samples provided with the Reporting Services installation and the samples are located in the following directory in your Reporting Services installation: <Reporting Services>\Samples\Scripts

“Run Report” Script

The following script is meant to run the report by passing it parameters and saving the output to Excel. Out of the four (4) parameters required by the report, the values for three of the parameters are hard-coded in the script file. The fourth parameter expects a value to be provided at runtime during the running of the script.

Microsoft SQL Reporting Services – Running a Report from the Command Line' File: RunReport.rss 

Microsoft SQL Reporting Services – Running a Report from the Command Line
Dim format as string = "Excel"
Microsoft SQL Reporting Services – Running a Report from the Command LineDim fileName 
as String = "C:\Export2.xls"
Microsoft SQL Reporting Services – Running a Report from the Command LineDim reportPath 
as String = "/SalesDashboard/Overview"
Microsoft SQL Reporting Services – Running a Report from the Command Line
Microsoft SQL Reporting Services – Running a Report from the Command LinePublic Sub Main()
Microsoft SQL Reporting Services – Running a Report from the Command Line
Microsoft SQL Reporting Services – Running a Report from the Command Line    
' Prepare Render arguments
Microsoft SQL Reporting Services – Running a Report from the Command Line
    Dim historyID as string = Nothing
Microsoft SQL Reporting Services – Running a Report from the Command Line    Dim deviceInfo 
as string = Nothing
Microsoft SQL Reporting Services – Running a Report from the Command Line    Dim showHide 
as string = Nothing
Microsoft SQL Reporting Services – Running a Report from the Command Line    Dim results() 
as Byte
Microsoft SQL Reporting Services – Running a Report from the Command Line    Dim encoding 
as string
Microsoft SQL Reporting Services – Running a Report from the Command Line    Dim mimeType 
as string = "ms-excel"
Microsoft SQL Reporting Services – Running a Report from the Command Line    Dim warnings() AS Warning 
= Nothing
Microsoft SQL Reporting Services – Running a Report from the Command Line    Dim reportHistoryParameters() As ParameterValue 
= Nothing
Microsoft SQL Reporting Services – Running a Report from the Command Line    Dim streamIDs() 
as string = Nothing
Microsoft SQL Reporting Services – Running a Report from the Command Line    rs.Credentials 
= System.Net.CredentialCache.DefaultCredentials
Microsoft SQL Reporting Services – Running a Report from the Command Line
Microsoft SQL Reporting Services – Running a Report from the Command Line    
' Report Parameters 
Microsoft SQL Reporting Services – Running a Report from the Command Line
    Dim parameters(3) As ParameterValue
Microsoft SQL Reporting Services – Running a Report from the Command Line    parameters(
0= New ParameterValue()
Microsoft SQL Reporting Services – Running a Report from the Command Line    parameters(
0).Name = "SalesType"
Microsoft SQL Reporting Services – Running a Report from the Command Line    parameters(
0).Value = "Auto"
Microsoft SQL Reporting Services – Running a Report from the Command Line    parameters(
1= New ParameterValue()
Microsoft SQL Reporting Services – Running a Report from the Command Line    parameters(
1).Name = "Country"
Microsoft SQL Reporting Services – Running a Report from the Command Line    parameters(
1).Value = "Japan"
Microsoft SQL Reporting Services – Running a Report from the Command Line    parameters(
2= New ParameterValue()
Microsoft SQL Reporting Services – Running a Report from the Command Line    parameters(
2).Name = "Year"
Microsoft SQL Reporting Services – Running a Report from the Command Line    parameters(
2).Value = "2004"
Microsoft SQL Reporting Services – Running a Report from the Command Line    parameters(
3= New ParameterValue()
Microsoft SQL Reporting Services – Running a Report from the Command Line    parameters(
3).Name = "Month"
Microsoft SQL Reporting Services – Running a Report from the Command Line    parameters(
3).Value = MonthParameter
Microsoft SQL Reporting Services – Running a Report from the Command Line    results 
= rs.Render(reportPath, format, _
Microsoft SQL Reporting Services – Running a Report from the Command Line        Nothing, Nothing, parameters, _
Microsoft SQL Reporting Services – Running a Report from the Command Line        Nothing, Nothing, encoding, mimeType, _
Microsoft SQL Reporting Services – Running a Report from the Command Line        reportHistoryParameters, warnings, streamIDs)
Microsoft SQL Reporting Services – Running a Report from the Command Line
Microsoft SQL Reporting Services – Running a Report from the Command Line    
' Open a file stream and write out the report 
Microsoft SQL Reporting Services – Running a Report from the Command Line
    Dim stream As FileStream = File.OpenWrite(fileName)
Microsoft SQL Reporting Services – Running a Report from the Command Line    stream.Write(results, 
0, results.Length)
Microsoft SQL Reporting Services – Running a Report from the Command Line    stream.Close()
Microsoft SQL Reporting Services – Running a Report from the Command Line
Microsoft SQL Reporting Services – Running a Report from the Command LineEnd Sub
Microsoft SQL Reporting Services – Running a Report from the Command Line
Microsoft SQL Reporting Services – Running a Report from the Command Line
'End of script 
Microsoft SQL Reporting Services – Running a Report from the Command Line

Microsoft SQL Reporting Services – Running a Report from the Command Line

Running the Script

To run the script, and pass the fourth parameter (MonthParameter) at runtime, issue the following command at the command prompt –

Microsoft SQL Reporting Services – Running a Report from the Command Liners –i RunReport.rss –s http://localhost/reportserver -v MonthParameter=”9” 

 

The -v flag above declares a global variable called MonthParameter and initializes it with the value “9”. This global variable is used in the script to set value to the Month parameter as parameters(3).Value = MonthParameter

相关文章: