U2U Blog

for developers and other creative minds

Building custom reports for SQL Server management

Recently I presented a session at the ITPROceed conference on ‘Administration intelligence’: using Business Intelligence solutions for analyzing log files, generate documentation, do disk quota estimations and other typical administration tasks. You can find the full presentation at SlideShare.

In this blog post, I want to focus on one of the techniques explained in this talk: creating Reporting Services reports to run in Management Studio.

SQL Server Management Studio reports

SQL Server Management Studio contains a bunch of predefined reports. In the Object Explorer, right click any database, select Reports –> Standard Reports and you get a listing of nearly 20 predefined reports:
image

 

If we select any of these reports, a new tab open in Management Studio in which the report renders. There are a few special things to notice about these reports:

  1. They can be exported in PDF, Word and Excel format, as well as being printed
  2. It can be a multi-page report. There is no explicit way to go to the next or previous page, but if you scroll past the end of the current page, you go to the next page.
  3. The report is context sensitive: if you open the same report by clicking on a different database, it shows information for that specific database.

Before we dive into building our own reports, I just wanted to point out that we have these predefined reports not only at the level of databases, but also at the server instance level, as well as on the Data Collection node (Management), the Integration Services Catalogs (from 2012 on) and the SQL Server Agent node. Just go ahead and try these predefined reports: They provide very useful information without the effort of building reports yourself… plus they might inspire you in building your own reports.

From report consumer to report producer

These predefined reports can make our life easier, but sometimes you want a report different from these predefined reports.Building a custom report is easy.

Step 1: Retrieving the data

The first (and often most difficult) step in creating any report is in retrieving the right data. Luckily SQL Server provides tons of information via the many management views. Just start typing SELECT * FROM sys. and the completion tool will show you the huge list of possible completions. If you want to read more on these before you continue, checkout the web, e.g. mssqltips.com has some interesting reading material for you.

In this example we want to report on log file sizes: what are the biggest log files I have on any server, and home many room for growth (or shrinking) do we still have in there.

A query with which we can get started is this one:

SELECT *
FROM sys.dm_os_performance_counters
WHERE counter_name IN (
'Log File(s) Size (KB)'
,'Log File(s) Used Size (KB)'
)
AND instance_name <> '_Total'
This returns a table such as this:
image
 
But in reporting it is important to present the data as easy digestible as possible. It is easier to build reports if different measures (in our case log size and log used size) are in different columns versus all in one single column (as they are right now). The PIVOT statement in T-SQL might help us out on that. Also, simple resorting and renaming might improve the readability of the result. So after a bit of a rewrite we end up with this query:
SELECT [Database Name]
,[Log File(s) Used Size (KB)] AS LogUsedSize
,[Log File(s) Size (KB)] AS LogTotalSize
FROM (
SELECT RTRIM(instance_name) AS [Database Name]
,cntr_value AS [Size in Kb]
,RTRIM(counter_name) AS [Counter Name]
FROM sys.dm_os_performance_counters
WHERE counter_name IN (
'Log File(s) Size (KB)'
,'Log File(s) Used Size (KB)'
)
AND instance_name <> '_Total'
) AS src
PIVOT(SUM([Size in Kb]) FOR [Counter Name] IN (
[Log File(s) Size (KB)]
,[Log File(s) Used Size (KB)]
)) AS pvt
ORDER BY [Log File(s) Size (KB)] DESC
image
 
Just copy the query in the clipboard, we will need it for the next step:

Step 2: Building the actual report

To build a report which we can run in Management Studio, we need to have SQL Server Data Tools (SSDT) installed (the tools formerly known as Business Intelligence Development Studio, BIDS, so you can use BIDS as well if you’re not yet on 2012). This is part of the regular SQL Server setup, so you can find it on your SQL Server 2012 or later installation media. But you can also just go and download it from the Microsoft Download Center.
After downloading, installing and starting this tool (which is just a Visual Studio template), we instruct it to create a new project. In the list of installed templates, you should find back the Business Intelligence group, select the Reporting Services group within, and on the right, click the Report Server Project Wizard and provide a useful project name:
image
 
Once you click OK, the wizard will start. Click Next to get past the start screen.
On the next screen the wizard asks which type of database connection we want to have, and to which server and database we want to connect. This is the tricky part, because we actually want to connect to whatever server and database we click upon in Management Studio object explorer. But now, we first need to develop this report in SSDT. Luckily, there is an easy way out: If we use the Microsoft SQL Server data source type, whatever server and database we select will be replaced with the ‘current’ server and database whenever we run this from Management Studio. So lets just connect to a server (I’m using localhost over here, but any server to which you can connect from SSDT will do):
image 
 
Notice that if you want your report to not query the current database but a fixed database (e.g. always master or msdb), you should use three part names in your query (e.g. SELECT * FROM msdb.dbo.suspect_pages).
 
Next screen asks for the query to retrieve the data for this report. Paste the query we’ve written earlier in this blog into this window. The next screen asks for a report type, just confirm the tabular report. Then we have a screen that asks for the report design. Just drag all three fields into the Details window and click Finish. Then we hit the last wizard screen where we give this report a meaningful name: Log Size. Click Finish.

Step 3: fine-tuning the report

After we finish the wizard we end up in the editor. Click the preview tab to get an impression of what the report will look once deployed:
image
 
This report only looks slightly better than the table we got in Management Studio earlier on. We need to make a couple of improvements.
Switch back to the Design tab to widen the database name column (just drag the column header to the right).
Finally we want to have a graphical indication on the size of these log files. To achieve this, right-click the rightmost column header in our report and add a column to the right:
image
 
Next, from the Toolbox window, drag the Data Bar component into the bottom cell of this newly created column. In the pop-up window, just confirm the default data bar type. Finally, click the blue bar that appears in this cell. On the right another pop-up window appears. Click the green plus next to Values and click LogTotalSize:
image
 
Now preview the report again:
image
 

Step 4: Run in Management Studio

Now that we have a report which is easy to read, let’s make sure we can easily open up this report in Management Studio. In SSDT, in the File menu select Save Log File.rdl As... In the save dialog, navigate to My Documents > SQL Server Management Studio > Custom Reports and save it over there. Notice that this is not required, Management Studio can open reports from any file location; It’s just the default location for these files.
Finally, we can now open up Management Studio, right click any object in the object explorer, select Reports > Custom Reports, and select the file we just saved there.
image
 
If you can connect to multiple instances of SQL Server, try out this report on all of them, and you will see how the report takes the server context into account.
image
 
Right click to print and export the report… you can have all the options we had on the predefined reports.
 
This is not the end of it, it’s just the start. There is a lot more to learn about building Reporting Services reports, as well as about all the useful information we can extract from SQL Server. So, give it a try!
 

Download

If you just want to download the .rdl file we created in this post, you can find it at http://1drv.ms/1yQjzMD

Comments (43) -

  • programmatore

    4/28/2015 9:13:02 AM |

    One thing I'd like to say is always that before purchasing more computer memory, look into the machine within which it will be installed. If the machine is usually running Windows XP, for instance, the particular memory limit is 3.25GB. Installing more than this would easily constitute a new waste. Be sure that one's mother board can handle this upgrade amount, as well. Interesting blog post.

  • programmatore php

    4/28/2015 12:08:28 PM |

    Thanks for your blog post. Some tips i would like to contribute is that computer memory should be purchased if the computer still can't cope with anything you do with it. One can put in two good old ram boards with 1GB each, as an example, but not certainly one of 1GB and one of 2GB. One should always check the company's documentation for one's PC to be sure what type of storage is needed.

  • eebest8

    5/7/2015 8:58:45 AM |

    Thank you for your blog.Really looking forward to read more. Cool.

  • cvbjnbb

    5/7/2015 1:47:29 PM |

    Kompetentny adwokat napisze dla Ciebie rzetelne pozew a dobry fotograf zrobi urocze zdj&#281;cia nawet na ulotki w warszawie jak równie&#380; dla twojej firmy na marketing. Potrzebna i wymagana jest skuteczna windykacja oraz wszechstronne i tanie ubezpieczenie gdzie nie jest wymagane dochodzone odszkodowanie za wykonane dobre dobry gdzie w pomieszczenia wstawisz na wymiar meble w zakupionej przez Ciebie nieruchomo&#347;ci. Wszechstronny producent, który wykona dla Ciebie ka&#380;de zamówienie, a jego produkty mo&#380;esz naby&#263; w hurtownia lub w wyszukanym sklep aleki kupisz sobie w apteka a paznokcie i fryzure zrobisz w studio pozdrawiam warszawa maj&#261;c fundusze i skutecznie uzyskane dotacje dzieki tojej wspania&#322;ej kancelaria która kupi&#322;a dobre i bezpieczne opony. Lepiej wcze&#347;niej szuka&#263; rzeczy na prezent i praktyczny i ciekawy upominek warszawa warszawa warszawa warszawa warszawa warszawa warszawa warszawa warszawa warszawa warszawa warszawa warszawa warszawa warszawa warszawa warszawa warszawa warszawa warszawa warszawa warszawa warszawa

  • https://www.fiverr.com/eebest8

    5/8/2015 12:26:04 PM |

    Thanks for the article.Really thank you! Fantastic.

  • gay blow job

    5/10/2015 3:33:42 AM |

    Really informative blog post.Thanks Again. Really Cool.

  • gay tube

    5/10/2015 3:35:53 AM |

    Im grateful for the article.Really looking forward to read more. Really Cool.

  • stefanmuaythai

    5/10/2015 7:46:59 AM |

    I truly appreciate this blog article.Really thank you! Much obliged.

  • stefanmuaythai

    5/10/2015 7:49:17 AM |

    Thanks for the article.Really thank you!

  • blog link here

    5/11/2015 10:46:33 AM |

    Thank you ever so for you blog post. Cool.

  • pop over to this web-site

    5/11/2015 10:48:51 AM |

    Thanks a lot for the post. Really Cool.