Pages

Saturday, February 26, 2011

SSRS: Scheduling Reports Using SQL Server Reporting Services 2008

This blog post gives detailed step-by-step instructions on how to schedule reports to run using SQL Server Reporting Services 2008 Standard Edition. Additional features are available in the Enterprise Edition and these will be covered separately.


Prerequisites
The example Reporting Services project used throughout this guide (put together using Bussiness Intelliegnece Development Studio) is available for download here. You might want to download and unzip this file, load it into the Business Intelignence Development Studio and deploy the reports to your server before attempting to continue with this step-by-step guide.

Accessing Reporting Services
In order to access the Self Service interface for reporting services you need to point your web browser to the page:

https:///reports

You will then see a list of folders for the reports available on that server and the "Data Sources" folder which contains shared data sources (that we won't be using during this demo).


Click on "GoogleKnolSSRSProject".


You can now see three reports; ComplexParameters, SimpleParameter, and NoParameter.

Scheduling A Report With No Parameters
To start things out simply we'll begin by scheduling the "NoParameter" report. Click on it.


This is the report itself (it simply displays a label showing information about this report). Click on the "New Subscription" button at the top left.


This screen allows you to create a new Subscription for this report. As you can see you have to specify two things; the report delivery options (how you want to receive it), and the schedule for delivering the report to you.

The delivery options avaialble to you will depend on how the SQL Server you are using has been setup. The default (if available) is via email but I'll take you through both email and delivery to a fileshare (which is sometimes more convienient if you're looking at something that is accessed by multiple people for example).

Deploying the Report via E-Mail
Select "E-Mail" in the drop down:


The screen will change to show you options available for an email. These are all pretty self-explanatory so I don't intend to go through them one at a time. For the purposes of this example I'm going to be outputting to report into Excel so change the "Render Format" from MHTML (web archive) to "Excel".

It's worth noting that the version of this screen you see is dependant on the permissions you have on the server. If you are a "normal user" then you get the screen shown above. If you have extra permisisons you will see extra fields (i.e. CC, BCC, Reply-To, Comment, and the "To" field becomes editable so you can send the subscription to multiple users).

Now that what we want to do is defined click "Select Schedule":


This new screen presents the options you have in the scheduler. For the purposes of this test I'm going to select this report to run on weekdays a few minutes in the future (just so it runs while I'm writing this!).

At the bottom of this screen is an option to have a start/end schedule for the report:


I won't be using this but feel free to experiement.

Once you've selected the schedule you want click "OK".


You are now presented with the Delivery Options and Schedule you've created. Click "OK" to start the subscription.

After the subscription time has passed an email will arrive with the report:


Deploying the Report via a File Share
Select "Windows File Share" in the "Delivered by:" drop down:


Pick a schedule and click "OK".

NOTE: SQL Server needs to be able to "see" the share to which you're trying to write the file. If it can't (for whatever reason) you will see a message like:


You will need to contact the person reponsible for Administering your server in order to get these permissions problems resolved (especially if you are trying to write this to a "private" share).

Other Deployment Options
Other options are available depending on how your SQL Server is configured (for example if your SQL Server is running in SharePoint integrated mode).

Scheduling a Report with a Simple Parameter
Return to the report selection screen:


Select the "SimpleParameter" report:


Unlike the pervious report you're now being prompted for a parameter before the report is executed. Enter the value "5" and then click "View Report". As you can see this report is pretty similar to the previous one except that it displays the value of the parameter passed to it. Click on "New Subscription" at the top:


This screen is exactly the same as the previous screen so I don't intend going over the Report Delivery Options (top) and the Subscription Processing Options (middle) again. The new section at the bottom is "Report Parameter Values". This section allows you to specify parameters than should be used to run the report.

Enter the value "7" and setup an appropirate schedule and then click "OK".

At the scheduled time the report will be processed and you will receive an email:


You'll notice that leaving the Render format set to "MHTML (web archive)" gives you a nice email in outlook that you can see without having to open an attachment (which can be a problem on certain mobile devices).

Scheduling Reports with Complex Parameters
Return to the report selection screen:


Select the "ComplexParameters" report:


As you can see this report takes four parameters; Day and Time and both pciked from a list (day has a default value, time does not), and Attendees and Guests are simple paremeters (attendees has a default, guests does not).

Pick the time "08:00-11:59" and enter "2" for guests and click "View Report":


Again this report simply re-displays the values of the parameters passed into it. Click on "New Subscription":


The "Report Parameter Values" section at the bottom of the Subscription screen allows you to use the drop downs to pick values for Day and Time OR, in the case of Day, to check the "Use Default" checkbox.

Check the two "Use Default" checkboxes (for Day and Attendees) pick the time 08:00-11:59 and enter "1" for Guests set the Report Delivery Options and pick a schedule and click "OK".

After a few minutes an email will arrive:


The advantages of using Default parameters for scheduling is that, for example, if you are running monthly reports for a full month then if the Default value for the parameter is "last month" then by selecting to use the default when the value of "last month" (i.e. Jan > Feb > Mar > etc) changes the report will automatically pick up the change. If, in February, you ran the report and specified January when the report ran again in March it would just run again for January.

Tuesday, February 15, 2011

SSRS: Working With Multiple Value Parameters


It is generally a good idea when writing reports to provide some space on the report to display the parameter values the user is using to run the report. It makes it a lot easier to deal with helpdesk requests along the lines of "Why's it doing this?" accompanied by a screen shot or a saved report - if you include the parameter values in the report then it should be fairly easy to duplicate it.

The problem comes when you're dealing with Multiple Value Parameters. For example if I look at the General Ledger Accounts we have setup in our ERP System we have around 804. Most users will select a couple, all, or one to report on. How do we make sure that enough meaningful information is displayed to help us debug any issues with the report?

The following function follows some simple rules;

  • If the number of selected records is the same as the number of records available in the dataset then display "All",
  • If a single record is selected then display it,
  • If more than 10 records are selected display "Multiple", and otherwise
  • Build a comma-separated list of the selected labels making sure that the last item is separated by ", or " rather than a just a "," (it does this by joining together all the selected items separated by "@@@@@", removing the last item and then adding it back in with the separator)

The code to process these rules is;

=IIF(
  Count(Fields!KEY.Value, "LIST_GLACCOUNTS") =
        Parameters!ACCOUNTLIST.Count, "All",
    IIF(
       Parameters!ACCOUNTLIST.Count = 1,
       Join(Parameters!ACCOUNTLIST.Label, ""),
       IIF(
         Parameters!ACCOUNTLIST.Count > 10,
         "Multiple",
         Replace(
           Left(
             Join(
               Parameters!ACCOUNTLIST.Label, "@@@@@"),
             InStrRev(
               Join(
                 Parameters!ACCOUNTLIST.Label, 
                   "@@@@@"),
               "@@@@@") - IIF( Parameters!ACCOUNTLIST.Count < 2, 0, 1)),
           "@@@@@", 
           ", ") + ", or " + 
           Parameters!ACCOUNTLIST.Label(
             Parameters!ACCOUNTLIST.Count-1))))

SSRS: Commonly Used Date/Time Functions


The list below is a few simple functions which I've come across for generating various dates based on the current date/time (using the Now() function).

Hopefully they'll prove useful and as new ones come up I'll update this post;

Last day of the current year,
=DateSerial(Year(Now()), 12, 31)

Last day of previous month
=DateAdd(""d"", -1, DateSerial(Year(Now()), Month(Now()), 1))

First date of previous month
=DateAdd(""m"", -1, DateSerial(Year(Now()), Month(Now()), 1))

First day of last year
=DateSerial(Year(Now())-1, 1, 1)

Last day of last year
=DateSerial(Year(Now())-1, 12, 31)

Last day of this year
=DateSerial(Year(Now()), 12, 31)

First day of this year
=DateSerial(Year(Now()), 1, 1)

SSRS: Common Issues with #Error and IIF

Is there anything more frustrating then seeing this non-error message and wondering what of massively complicated function you've just entered is causing the problem (let's not forget that the Expression editor in Report Builder 3 doesn't allow you to do multi-line statements - just to make it easier!). 

Drives me wild.

Having worked with other people to debug functions the one thing that seemed to really help people was giving them a better understanding of how IIF worked (oddly IIF was the cause of *most* of the problems!).

The IIF function (and the key word here is "function") requires ALL the values passed to it to be valid.

For example if you try IIF( a > 0, 200/a, 0) you will get a "Divide By Zero" error when the value for a is zero because SSRS will still try and evaluate 200/a even though it will never be displayed.

The (clumsy) way around this would be to change the statement to;

=IIF(a > 0, 200/IIF(a = 0, 1, a), 0)

This would prevent the error by setting a to 1 when it is used as the divisor. 

You should also watch out for this problem when using InStr or InStrRev in conjunction with a function to get a substring (as, for example, asking for the 9-character of a 8-character string will give you the dreaded #Error).

If ever there was a bunch of functions crying out for a default value to return in the event of an an error it's these!

Tuesday, January 11, 2011

Installing Active Directory Tools Under Windows 7

This blog post is a step-by-step guide to installing the Active Directory Tools (i.e. Active Directory Users and Computers) on a Windows 7 machine. It has been tested on Windows 7 Enterprise but will probably work with Professional or Ultimate as well - Home users it will not work (but then why are you wanting to administer AD from a home machine??!!)

First of all you need to download the software from Microsoft. In the Microsoft Download Centre these are called "Remote Server Administration Tools for Windows 7" a direct link to the download page is given below;

http://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=7d2f6ad7-656b-4313-a005-4e344e43997d

If you scroll down to the "Files in This Download" section of the page you'll see two files. Depending on whether or not you're running 32-bit or 64-bit Windows 7 you need to pick the correct file. If you're unsure of which version you're running then go to the start button, right-click "Computer" and then select "Properties". You'll see something like this;
System Information (64-bit/32-bit)
Look at the "System type:" value and you'll see what version of Windows you're running.

If you're running 32-bit then you need to download the file which starts "x86..." (currently this is "x86fre_GRMRSAT_MSU.msu" but it might change). For 64-bit users you need to download the file which begins "amd64..." (currently this is "amd64fre_GRMRSATX_MSU.msu") - this is true even if you're running a non-AMD 64-bit processor. The reason for this I'll leave Microsoft to explain ...

Once you've got the file install it (it's a standard KB update file).

After it's been successfully installed go to the Start Menu > Control Panel and select "Programs";
"Turn Windows Features on or off" under "Programs and Features"
The "Windows Features" dialog box will be displayed, scroll down to "Role Administration Tools" (under "Remote Server Administration Tools") and select the the following items;
"Windows Features" dialog
Click "OK" to make the changes.

In order to make finding these under the Start Menu a little easier right-click the Start Button and select "Properties";
Taskbar and Start Menu Properties
Select "Customize ..." and then scroll down the list until you see "System administrative tools" and choose where you want the tools to display;
Customize Start Menu
Under the Start Menu you will now see an "Administrative Tools" option, under this you'll see the new AD Tools;
Active Directory Start Menu Items
NOTE: Sometimes a reboot is required to pick up these changes!

Wednesday, December 8, 2010

How To Get To The Arbury Carnival


This blog pos covers a variety of transport options for getting to/from the Arbury Carnival. This event is held in King's Hedges in North Cambridge (UK) on the second Saturday in June each year.


So where is it?
The following Google Map shows the location of the Carnival.
If you want to search for the nearest postcode then the best one to pick is probably CB4 2LD. Campkin Road, the postal address for the Carnival, is actually quite long and if you type in that postcode coming from the North or West will actually cause you to "arrive at your destination" without your destination actually being visible. Hardly ideal!

Travelling By Car
Clearly with the congestion caused by the Carnival you don't really want to be travelling to/from the area by car but I accept that for some people it is not just the easiest but the only option. There are two off-street car parks within very easy walking distance (one right next to the Carnival green itself).

Arbury Community Centre Car Park
Situated right next to the Arbury Community Centre (which in turn is right next to the Carnival green) this is the most convienient and consequently it's the one that's going to fill up first. It's actually quite small at around 50 cars total and it's likely that stall holders (who will be arriving at 8am) are likely to have taken up a lot of the spaces so I guess the key is to arrive early!

There are several disabled parking bays.

NOTE: Please take note of the Height Restriction entering the car park! This is fairly common in Cambridge and driving into it will really ruin your day ...

Arbury Community Centre Car Park (from Google Street View)  
One final note on car travel; please try and avoid parking on Northfield Avenue - the procession passes down this route and it makes it "difficult" if for the last hundred yards the people involved have the run the gauntlet of parked cars.

Arbury Court Car Park
Slightly further way and with about the same capacity this Car Park serves the near by shopping precinct of Arbury Court. It is similarly sized and so consequently will fill up similarly quickly.

Arbury Court Cark Park (from Google Street View)
You can walk straight from the back of the Car Park across the road (at traffic lights), past the Arbury Community Centre and onto the Green where the Carnival takes places (2-3 minutes walk).

Again there are several disabled parking bays.

Travelling By Bus
Arbury/ King's Hedges are well served by Stagecoach Bus services. By far the most convienient bus service is the Citi 1 which runs from Fulbourn to Arbury/King's Hedges via Addenbrookes, Cambridge Station, the City Centre, and Cambridge Regional College every 10 minutes during the day.

The timetable for the Citi 1 is available from Stagecoach via their website here.

Travelling By Guided Bus
The Guided Bus passes very close by (5-10 minute walk) from the site of the Arbury Carnival. The closest stop on the Busway is either the Cambridge Regional College or Orchard Park East. 


The arrows at the top mark the Orchard Park East and Cambridge Regional College stops, the X at the bottom/ middle of the map is where the carnival is located. You can either walk down King's Hedges Road/ Northfield Avenue or (as you've already paid for the ticket) catch the Citi 1 service from opposite the Regional College/ at the Top of Northfield Avenue and it will take you straight to the Carnival site.

Travelling By Train
The nearest station to the Carnival is Cambridge Station. The Citi 1 bus service runs (every 10 minutes) direct from the station to the Carnival site (the jouney will take around 30 minutes). See above for instructions on travelling by bus.

Friday, November 26, 2010

Oracle PL/SQL: Dropping All Objects In An Oracle Schema

This blog post provides a simple piece of PL/SQL which will delete every object owned by the currently logged in user.

Here is the PL/SQL block;


declare
  v_ItemCount integer;
begin
  SELECT count(*)
    INTO v_ItemCount
    FROM ALL_OBJECTS AO
   WHERE AO.OWNER = USER
     AND AO.OBJECT_TYPE NOT IN ('INDEX')
     AND AO.OBJECT_NAME NOT LIKE 'BIN$%';
  while (v_ItemCount > 0) loop
    for v_Cmd in (SELECT 'drop ' || AO.OBJECT_TYPE || ' ' || AO.OBJECT_NAME ||
                         DECODE(AO.OBJECT_TYPE,
                                'TABLE',
                                ' CASCADE CONSTRAINTS',
                                '') as DROPCMD
                    FROM ALL_OBJECTS AO
                   WHERE AO.OWNER = USER
                     AND AO.OBJECT_TYPE NOT IN ('INDEX')
                     AND AO.OBJECT_NAME NOT LIKE 'BIN$%') loop
      begin
        execute immediate v_Cmd.dropcmd;
      exception
        when others then
          null; -- ignore errors
      end;
    end loop;
    SELECT count(*)
      INTO v_ItemCount
      FROM ALL_OBJECTS AO
     WHERE AO.OWNER = USER
       AND AO.OBJECT_TYPE NOT IN ('INDEX')
       AND AO.OBJECT_NAME NOT LIKE 'BIN$%';
  end loop;


  execute immediate 'purge recyclebin';
end;

The SQL was written against Oracle 10g (hence the "purge recyclebin" at the bottom and the exclusion of objects already in the recycle bin from the "drop" loop). You can monitor progress (and check to see if the code has stalled) by running the count SQL in another editor attached to the same schema.

Clearly a piece of SQL you should be very careful with!