Using custom code in SSRS

Step by step to add custom code in SSRS

Introduction

SSRS custom code extends re-usability of certain logic in multiple places inside a report. It also helps get rid of writing lengthy calculations in many places with a function. SSRS 2005/2008R2 allows us to write custom codes in VB. If somebody is familiar with C# can choose another option called - Custom Assembly.

We will create a sample report with a simple Custom code which will accept two numbers and return the greater of two. So for the sample report I have a given table  Test table with three columns A,B,C


Now we will try creating a report from the source data so first we will add a new report in BIDS SSRS project. create proper data source and data set referring to the above table.
Now first we will go to Report Property and will click on the Code portion and write the below code.

After writing the code we will drag a table and will specify all the columns and will add one extra column that will return the Greater of two values. So in the last column we right click and click on the expression as below.

Now finally we add the below expression will calls the function we defined in the property section of the report.

We see that I have used Code.functionname as when we define a function the SSRS engine puts the function inside the Code class and thus we need to refer the Code before calling the function.
Now we execute the report and see the value returned is the Greater of B & C in last column.



Using merge join without Sort transformation

Merge join without SORT Transformation
Merge join requires the IsSorted property of the source to be set as true and the data should be ordered on the Join Key. So when we add a SORT transformation it sets the IsSorted property of the source data to true and allows the user to define a column on which we want to sort the data ( the column should be same as the join key). Now to avoid the using SORT transformation we need to set the metadata of the source properly for successful processing of the data else we get error as IsSorted property is not set to true.

We will try to join two tables Department and Employee on DeptID column without using SORT transformation in our SSIS package.

Department Table details



Employee Table details
Steps in SSIS package

  • Create a new package and drag a dataflow task.
  • Now right click on Data-flow and click on edit, the data-flow container opens.
  • First task is to create a connection to the database.   
  • Now we need to set department source, first we will drag the OLEDB Source and set connection and  table

  • Now we click on "ok" to save changes. Again we right click on department source and click on "Show Advance Editor" and go to "input output property" tab. On the "input and output property" tab we select the "OLEDB Source output" and set the IsSorted property to true as mentioned below.


  • After setting the above property we expand the "output column" and inside that we select the column (DeptID) which we will use in join and set SortKeyPosition = 1 in the properties tab as shown in below image.

  • We will repeat the same process for second source i.e the Employee table.




Once the properties set for the sources we drag the merge join transformation and set the required properties which include the sources, join key and the output columns as shown below.


Now we drag the out put of the Merge join and join to a Derieved column where in the path we also configure data viewer to check the data. Now out package Data-Flow looks like the below image.


We run the package and try to view the data which shows like below.




















T-SQL LEAD LAG and SUM function based query

  Query on T-SQL window clause Below is the sales table Order_Date Name Product SubCategory ...