Thursday 29 January 2015

SSRS Report: Cannot see the field from dynamic stored procedure

I have to create a cross-tab report

  1. Where the spreading columns have to created dynamically
  2. Add two calculated column - GrandTotal row, and Total Column

Required layout:

Applications by school and status code             App Year: 2015                   Ref Date: 22/01/2015


School
Total
A00
A01
A02
As many columns as needed (Dynamic)…
Acton High School
3
2
1


Barton School
8
4
2
2

Calvin Academy
2

1
1







As many rows as there is schools…





Totals
13
6
4
3



Parameters:
  1. Application Year – Defaults to the application year of the current year (presently 2015) – option to go back to previous years
  2. Reference Date – Defaults to current date, but will need to be changed if the user changes the Year parameter
  3. Threshold, Application status total threshold - Please include a filter option to only show schools where the total applications is above a certain number.  Suggested values: 1 (Default),2,5,10,15,20,30,40,50

My Approach towards creating this dynamic SQL report :
  1. Create a stored procedure with dynamic SQL which will have the follow properties
    1. It will accept three parameters. Parameters will be initialized with default values.
    2. Declare a variable which will store the value of the spreading column
    3. Declare a variable which will add the sum() on every spreading column generated by dynamic SQL in step 2 which will calculate the GrandTotal row.
    4. Create a dynamic PIVOT query and inside the PIVOT query
      1. To calculate the TotalColumn, Use a SQL window aggregate function OVER with aggregate function COUNT
      2. SELECT INTO a temporary table
      3. Terminate the PIVOT query with a semicolon inside the same scope
      4. Now issue the SELECT query against the temporary table created in here step 2. Add a RANK column in this SELECT statement.
      5. Union the result set from here in step 4 with the dynamic GrandTotal row
    5. Execute the dynamic pivot query
  2. Create a SSRS report
    1. Create a DATASET
      1. Give a dataset name
      2. Choose a data source
      3. Choose Query Type: Text, do not choose stored procedure. If you choose stored procedure it will not return any columns or fields in report. In the query designer type the stored procedure name with parameter name each separated by comma. For example: exec xProcApplicationsBySchool @RefDate, @ApplicationYear
      4. Check the parameter tab. In case parameters are not automatically created then add the parameters manually
      5. Create a filter in case you have not pass some query parameter. The main difference between the query parameter and filter is, query parameter will filter the data in the dataset where as if you use filter the data will be in the dataset but when you will run the query then it will use the query parameter. The benefit of filter is you can decide later if you want to filter the data or not where as query parameter already filter the data.

Some of the problems I have experienced while I was creating this report:
  1. Stored procedure was executing successfully but it was not returning any columns. To solve this
    1. SET FMTONLY OFF;
  2. Since it was dynamic sql stored procedure, all the sql statement was written inside a single inverted quotation mark, so when I needed to use a single inverted single quotation mark to show it is a string, it was giving an string conversion error message. 
    1. For example to write - WHERE city='London' in dynamic sql you have write - WHERE city='''London'''. Here one for set @variable=' ', one for string ' ', one for escape charater ''. 
    2. Cannot convert datetime to nvarchar. Same thing was happing when I tried to compare a date - WHERE application_date<=@RefDate.
      1. I had to convert the date to nvarchar 
      2. and write statement like this - WHERE application_date<='''+@RefDate+'''
  3. In SSRS report, 
    1. SSRS report was not showing any data set fields. 
      1. SSRS - Choose Query Type: Text, do not choose stored procedure. If you choose stored procedure it will not return any columns or fields in report. In the query designer type the stored procedure name with parameter name each separated by comma. For example: exec xProcApplicationsBySchool @RefDate, @ApplicationYear 
      2. In the “Stored procedure” itself –After this it was showing the fields but another problem had arisen. It was not using the query parameter. At first initialize the parameter in stored procedure then in SSRS-add the parameter manually from the parameter tab
    2. Same size all the columns
      1. Select both the cells (not the column), go to properties and enter the value in width. The width can be found under "Postion->Size" in properties. If for some reason you cannot see the Width/height under the Size, enter the values as "Width, height" in inches, example, "1.5in, 0.25in"
    3. Set the first row, and Last row different color
      1. For the first row =IIF(RowNumber("DataSet1") = 1, firstRowColor, OtherRowColor), 
      2. For the last row=IIF(RowNumber("DataSet1") = CountRows("DataSet1"), lastRowColor, OtherRowColor). 
      3. To combine the two=IIF(RowNumber("DataSet1") = 1, firstRowColor, IIF(RowNumber("DataSet1") = CountRows("DataSet1"), lastRowColor, OtherRowColor)). To see the properties, Click on View->Properties
    4. Add the Report header page for SSRS report, not the page header page
      1. Insert the header information just above the Tablix where you show your table fields value
    5. Push the Tablix into the second page.
      1. Go to Tablix properties-->General tab-->Check option add a page break before 
    6. Add a chart, Color code the highest value
      1. In order to color code the highest sum value or second highest value, adding a RANK column in the stored procedure
      2. How to add a Ranking column in SSRS
        1. Do it in the sql query for example stored procedure not in the SSRS. Use window aggregate function.For example ROW_NUMBER. 
        2. ROW_NUMBER was not working in according order by clause 
          1. Terminate the previous statement with semicolon worked
        3. ROW_NUMBER tie breaker was not working
          1. Both the tie breaker column has to mention – ascending or descending, for example ORDER BY SchoolTotal DESC, #PivotTable.CMPN_Company_Name ASC
      3. In SSRS expression - =IIF((Fields!Rank.Value)=1,"Yellow","Silver")
      4. In the chart, filter the chart data. For example show only schools with more than 10 TotalStatus
        1. Select Chart (Not any chart parts)-->Chart Properties-->Filter-->Add-->Choose a field, >=, 10, 
    7. After adding the RANK, GrandTotal is appearing in the middle because RANK sort the result set already by SchoolTotal, CMPN_Compmany_Name. 
      1. Add the GrandTotal in the SSRS report
        1. Insert Row-->Outside Group - Below
    8. Add a Tool tip for the Header column
      1. Select the textbox-->Properties-->General--Tool tip







0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home