简体   繁体   中英

Generate script to create reports for each record in SSRS

In SSRS you can use parameters to export a report to a specific format. For instance I can add to a url rs:format=PDF to get the report to export to a pdf file. When this exports the report comes in pdf file with the name of the report the title of the actual report. I realize there is no way in SSRS to actually give a specific name to the report when it is downloaded.

However, we have 10k records that a user wants automatically exported to say pdf export. I don't want to go in there and create 10k different data driven subscriptions and give the parameter the ID value of the record. I'd have to do that 10k times!

Is there an easier way to do this such that I can export all my records to a specific format with the name of the report the ID of the record?? For instance we use an auto-number id for each record so one pdf may be named 12.pdf and the next is 13.pdf, etc. But this should be run on demand and at anytime.

Is there anyone who knows if I can do this via tsql or a sproc?

Right click on your SSRS report and follow this path:

Manage > Subscriptions > New Subscription > Data Driven Subscription

Read here for more info on that. But long story-short, you set up the parameter as you described and then fill it in via sql cell.

Step by step:

  1. In the "subscriptions" pane, click "new subscription"
  2. In the "type of subscription" radio-buttons, be sure you click "data driven subscription" and not "standard subscription".
  3. In the "Destination" drop-down, select "windows file share".
  4. In the "dataset" section, click "edit dataset". Establish a connection, write a sql query. In your case, a 10000 row sql query that outputs a different "title" or similarly named column for each different report. You may also want to output a "fileName" field.
  5. Validate as necessary and click "apply" to return to the previous screen.
  6. In the "Delivery Options" section, select "get value from dataset" in the "Source of value" section for the file name, and reference the filename column created in step 4.
  7. Write in the path manually, select the render format manually, and complete any other options as necessary.
  8. In the report parameters, select "get value from dataset" for the title parameter you have in the ssrs report. Then reference the "title" field you created in step 4.
  9. Fill in other cells as necessary.
  10. Click "Create Subscription".

Here is a visual tour, with implementation for a different report of my own:

数据驱动的子初始

数据驱动的子查询

数据驱动的亚决赛

So you mention that you don't have enterprise edition, and it seems that only enterprise edition has data-driven subscriptions.

There is another way. You can execute SSRS via WebClient in C#.

First, capture the parameters dynamically via SQL:

class parameterRow {
    public string fileName;
    public string active;
}

... 

var parameterRows = new List<parameterRow>();

var sql = @"

    select      fileName = 'activeRecords.pdf', 
                active = 'Active' 
    
    union all
    select      fileName = 'inactiveRecords.pdf', 
                active = 'Inactive'
    
";

using (var con = new SqlConnection("server=someServer;database=someDB;trusted_connection=yes"))
using (var cmd = new SqlCommand(sql, con)) {
    
    con.Open();
    
    using (var rdr = cmd.ExecuteReader()) 
        while (rdr.Read()) 
            parameterRows.Add(new parameterRow {
                fileName = rdr[0].ToString(),
                active = rdr[1].ToString()
            });
            
}
    

Then loop the dynamically created parameters, downloading and zipping each iteration by integrating the parameter name into the url and the file name into the zip entry name:

using (var client = new WebClient()) 
using (var fileStream = new FileStream(@"c:\...\ssrs.zip", FileMode.Create, FileAccess.Write)) 
using (var zipArchive = new ZipArchive(fileStream, ZipArchiveMode.Create, false)) {

    foreach(var paramRow in parameterRows) {
    
        var url = Uri.EscapeUriString(
            @"http://ssrs.YourSite.org/ReportServer/Pages/ReportViewer.aspx?" + 
            @"/Basic Student Lists/Student List" + 
            $@"&Active={paramRow.active}" + 
            @"&SortOrder=Student Name" + 
            @"&rs:Command=Render&rs:Format=pdf"
        ); 

        client.UseDefaultCredentials = true;            
        var data = client.DownloadData(url);
        var zipEntry = zipArchive.CreateEntry(paramRow.fileName);

        using (var entryMs = new MemoryStream(data))
        using (var zipEntryStream = zipEntry.Open()) 
            entryMs.CopyTo(zipEntryStream);
        
    }
    
}       
        

Obviously you may not have to zip, but since it's part of the old code I fetched, I figure I'd keep that feature in case it's useful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM