Automating SQL server reports execution - sql-server

Hi I have question is there any way to automatically execute SSRS reports for example user goes to ssrs and put some parameters to execute reports and exports them in excel and pdf . Is there any way to avoid human interaction and automate the whole process or any job in C# code runs automatically reports and exports them in format.
Regards

A Reporting Services subscription is a configuration that delivers a report at a specific time or in response to an event, and in a file format that you specify. For example, every Wednesday, save the MonthlySales.rdl report as a Microsoft Word document to a file share.
Subscriptions can be used to schedule and automate the delivery of a report and with a specific set of report parameter values.
You can create multiple subscriptions for a single report to vary the subscription options; for example, you can specify different parameter values to produce three versions of a report, such as a Western region sales report, Eastern region sales, and all sales.
FROM: http://msdn.microsoft.com/en-us/library/ms159762.aspx
And for how to configure a subscription to deliver a report by e-mail, check here http://technet.microsoft.com/en-us/library/cc872783.aspx
And if you need to do programmatically, you can start here http://www.codeproject.com/Articles/36009/Programmatically-Playing-With-SSRS-Subscriptions

If you don't want to use subscriptions, you can write your own program in C# to pass the parameters and render the report and save in whatever format you like. Then run this using Windows scheduler or however you want to.
It is quite easy and Microsoft have some example code to get you started.

Related

Create PDF automatically after new entry in database

I am not sure if this is even the right site for this but I'll ask anyways.
Does anybody know a program that can create a PDF with data from a database with a complex SQL statement? When an employee finishes a request for a customer, I want that the program is triggered by the new entry in a database table and fills out a pre built PDF with data that it pulls from a database.
It needs to be a complex program that can process big SQL statements.
The only thing you can do to run custom code in SQL Server is to create a CRL Stored Procedure and a trigger to start PDF processing in your specific use case. You can write a class library that connects to the database you are triggered from using a specific keyword in the connection string. You can pass in the key you need to use to select the whole dataset you need to fill the PDF as a SP parameter.
https://msdn.microsoft.com/en-us/library/ms131094.aspx
In the class library you can reference third part libraries to interact with an editable PDF and fill the flieds you need with the data retrieved from the database. I suggest you to have a look about security concerns releted to CLR use in SQL Server. Basically your code runs within the SQLServer.exe process, sharing resources and access privileges.
https://msdn.microsoft.com/en-us/library/ms131071.aspx
SSRS can produce PDF. See if that table insert trigger can call a CLR that in turn will execute the report that can even be attached to an email.
The report can be as complex as you need, so long the report SP can populate the data based on the newly inserted row.
For stability, let's not have a trigger, but the insert process itself should be done in an SP, in which that insertion SP shall call the CLR.
Further, instead of using CLR, we can use SSRS Data-Driven Subscription. When making the Subscription, we can make it as a one time scheduled Job. The SP can invoke this 'expired' Job from SQL Server Agent by using sp_start_job.

How to make a SSRS work for multiple users

I can't find anything on this so I don't think I am asking the question right but here is my situation. I have a stored procedure which the end user passes a list of filter criteria, since I don't know what the filter criteria will be I used dynamic SQL. Further, to allow for more than one user to run the stored procedure concurrently I used all dynamically named temp tables so there would be no collisions. That all works.
Now my problem is how to output the report. Right now I have a SSRS report pointing to a single database that the stored procedure dumps its output to. When the stored procedure finishes the report is displayed. This works for one user but if two users run the stored procedure at the same time I have no way of knowing which output data will show on the report. Complicating issues is the dynamic user filter criteria can greatly effect the time the stored procedure takes to complete. I can see the report loading data just as the other stored procedure session is truncating or loading data to the output table.
I can queue up requests and run them one at a time but ideally I want them to be able to run concurrently as several users have to run this report many times at the beginning of each month. Is there a way to ensure that the data displayed on the report to the end user matches the data output from the stored procedure session the user ran.
EDIT:
The following is not a requirement but to clarify how this works now, the end user goes to an web site, enters in some filter criteria into a bunch of text boxes, one for each filterable database field, using a third parties search wildcards format that they already know. I then take that input, clean it up and parse it into a SQL WHILE string which is then passed to the stored procedure that gets the data. When the stored procedure finishes control is passed back to the website that then displays the report as an imbedded object. So the website is calling the parsing method, the stored procedure and the report.
SQL Server will create a separate session for each report user. It will then invoke the stored procedure in its own session (SQL Server is a multi session product) and using the selected parameters will run the proc and produce unique results which will then be passed back to the report user who invoked it and the report will then be shown to the user. If ten users simultaneously invoke the report, with unique parameters, they will each see a different report in the body of the report.
I ended up reworking the whole thing as Benjamin suggested and it works fine but that really was not the answer I was looking for. I finally stumbled upon it while researching something else. You can set the report viewer's DataSources.Add() method to add your own dataset. This would have allowed me to run the stored procedure in the webpage so I can catch and handle the errors at the webpage level so I could give more useful feedback to the end users. You can see this other post setting the datasource for a local report net report viewer for more detail.
The example is for a local report but I was able to get it to work on a remote report but I am on a trusted internal only network. As I understand it, you can also do it with the newer report viewers too.

Steps to export the SQL Server returned result set to an Excel file

I have a stored procedure that I am going to run every weekend, it produces a result set that I need to export into an Excel file.
For the above problem I want to automate this process, so I am going to create a SQL Job and I am going to run this stored procedure every weekend so that that generated Excel file is sent to my reporter.
For this I need steps to export the result set data to an Excel file.
And also is it possible to send that Excel file to the specific mail while running the job itself?
So, you might try your luck on https://dba.stackexchange.com/, but in my experience a SQL Agent job running a stored procedure could be coaxed to return CSV or XML - and those could end up in Excel, but there are missing links. I think the missing links would involve programming and potentially 3rd party tools to avoid using Excel's COM API.
I'd strongly recommend your pursuing SQL Server Reporting Services. It is included free with your edition of SQL and includes the ability to
run reports on a schedule (subscriptions),
format the results as an Excel file
distribute the results via email
You'd take your query and use it as the data source for a "report" and use the report wizard to create a very simple table with the results.
Avoid page headers (or footers) that span columns - this will keep the excel output cleaner.
References
Stack Overflow: reporting-services-export-to-excel-with-multiple-worksheets
Technet: Reporting Services

Reporting Solution for Data Validation Queries

I'd like to get some advice on a reporting situation that I have. I am working in SQL Server. I have a ton of data validation queries that I run against a database. In general, for each query, I return two things -- one is the count of the offending records, and the other is the offending records themselves.
My goal is produce a report that gives the counts of the offending records for all data validation queries (ideally, on one sheet in an Excel workbook) and the offending records themselves (ideally, on separate sheets in an Excel workbook).
How is this best achieved? That is, what technology is best for this situation? For example, in the past, I have prototyped the queries in SSMS, copied them into a Windows batch file (and added code to write the results to separate text files), and called the batch file via the sqlcmd utility (using command prompt). However, I know that other solutions exist (e.g., SSRS). Would something like SSRS be a better tool for this situation? I'm hesitant to go the SSRS route, since I'm only giving metrics on one issue (i.e., counts of offending records) and the rest of the report consists of offending records.
This might get closed because it is a matter of opinion, but SSRS would be a good solution for this requirement. I think SSRS is a good fit if you have the following criteria:
You need to visualize the data in some kind of a table, chart, or graph
You want to send out automated emails every morning / week / month to a group of users (as opposed to just individual consumption)
You want to be able to export the report to other formats (excel or pdf) for additional analysis or sharing.
Otherwise, if it's just for you and you currently don't have SSRS running on the server, save yourself the overhead of running another service and just keep doing it in batch files.

Switch tables based on parameters selected in CR 2008

I am using crystal reports 2008 and my req is If i select a parameters (like it be %) then it should use a different view if it is not the it should use the same view cn I do that using a query. in Crystal reports
I don't know a way to do this in Crystal. Perhaps in .net. If you use an application like Millet Software's Visual Cut to schedule, run and distribute your reports you can set it up in such a way that the method call that triggers the report job has the ability to override the default connection parameters. That itself can be a variable too - override or not based on certain criteria.
I've deployed it in a way that uses 1 report to recursively connect to a list of different (but same schema) databases to run versions of the report, one after the other.
FULL DISCLOSURE
I do not work for Millet Software, I do not receive referral bonuses and I do not receive a reseller bonus. I simply have had great experience with them and wanted you to be aware of the possibility, not solicit your business.
If you are using database, which supports scripts or stored procedures you can do this using a command or a stored procedure. Inside the script check the value of the parameter and call one or another query.
Sample code for SQL server will look like this:
if CHARINDEX(#Parameter,'%')>0
SELECT * FROM View1
ELSE
SELECT * FROM View2

Resources