About This Blog
Friday, 30 December 2011
Monday, 26 December 2011
Dynamic Sorting in SSRS
Many times while developing any report we come across a situation where we need to SORT the TABLE columns or the columns in the GROUP dynamically. There are many well known ways to achieve this. However, here I will demonstrate a not-so-known way -
Implementation
Assuming that the sorting is to be done on 3 columns – Col1,Col2 & Col3. But the ORDER is undecided until runtime. Now, what we can do is -
- Define 3 Report parameters. Say - @SortCol1Name,@SortCol2Name & @SortCol3Name of STRING type.
- Pass the name of the columns which need to sorted in the required order. Say -
- @SortCol1Name = “ColumnX”,@SortCol2Name = “ColumnY” and @SortCol3Name = “ColumnZ”
- OR @SortCol1Name = “ColumnY”,@SortCol2Name = “ColumnZ” and @SortCol3Name = “ColumnX”
- OR any preferred order
- In the Sorting Option of the Properties dialog box of Table or Group, set the sort expression as under -
- =Fields(Parameters!SortCol1Name.Value).Value
- =Fields(Parameters!SortCol2Name.Value).Value
- =Fields(Parameters!SortCol3Name.Value).Value
This finally gets converted to –Fields!ColumnX.Value,Fields!ColumnY.Value & Fields!ColumnZ.Value if we have the passed the following values for the parameters - @SortCol1Name = “ColumnX”,@SortCol2Name = “ColumnY” and @SortCol3Name = “ColumnZ”Conclusion
With this trick, we can easily set the sort expression dynamically. However, there is a limitation that we can not set the Sort Direction dynamically. I could not find out a way for it. If you have any trick to get it done with this, please leave your suggestion as a comment to this post.
Wednesday, 7 December 2011
How many times the T-SQL inside the CTE is executed?
Common Table Expression (CTE) has become very popular these days. However, many users of CTE still have a myth that the t-sql written inside the CTE will be executed only once irrespective of the number of times it is referred in the subsequent CTEs or the related query.
However, this is not correct. It is a pure misconception. And let’s try to prove this with a simple example.
Proof
The Query
;WITH CTE1 AS (
*
)
Please have a look at the execution plan. It clearly shows that even though the t-sql to fetch the data from the table is written only once inside CTE1, the Scan is done thrice. This is because CTE1 is referred thrice in the final query -
- Two times CTE1 is directly used.
- Once CTE1 is used indirectly via CTE2.
The CTE should be thought of a view that is defined for the current query only. At the time of execution, the query optimizer will replace all the direct/indirect use of CTE with the actual query in the same way as it does for Views.
Wednesday, 30 November 2011
Using OFFSET and FETCH
Introduction
Many times while developing our applications we feel the need of pagination, where our User Interface (UI) has to list a number of records and fetching them all at once and listing is not a feasible option because of the following reasons -
- High utilization of the network bandwidth which if on a higher side might even choke up the bandwidth.
- User is not guaranteed to see the latest details in a multi user environment.
- High need of RAM on local machine for caching/processing.
So, the solution which is generally implemented in this situation was to fetch only the relevant records from the backend. Until Denali the following were the options used to counter this situation -
- Before SQL 2005 – ORDER BY clause in combination with TOP
- From SQL 2005 onwards – ROW_NUMBER() function with a WHERE clause
And from Denali, we can use ORDER BY clause in combination with OFFSET and FETCH
Implementation
Let’s see how we can get the same output using all of the 3 ways explained above and try to fetch records from 3 to 4 assuming page size to be 2.
Total Records are as under
1. ORDER BY + TOP
DECLARE @PageNo AS INT
DECLARE @PageSize AS INT
SET @PageNo = 2SET @PageSize = 2SELECT*
FROM (SELECT
TOP (@PageSize) *FROM (SELECT
TOP (@PageNo * @PageSize) * FROM dbo.DemoTable DT
ORDER BY
ID ASC) XORDER BY
X.ID DESC) YORDER BY
Y.ID ASC
Output
2. ROW_NUMBER() + WHERE
DECLARE @PageNo AS INT
DECLARE @PageSize AS INT
SET @PageNo = 2SET @PageSize = 2;WITH Data AS (
SELECT*,
ROW_NUMBER()OVER(ORDER BY DT.ID ASC) Rno
FROMdbo.DemoTable DT
)
SELECT ID,NAME,CITY
FROM DataWHERERno BETWEEN ((@PageNo - 1) * @PageSize) + 1 AND ((@PageNo - 1) * @PageSize) + @PageSize
Output
3. FETCH + OFFSET
DECLARE @PageNo AS INT
DECLARE @PageSize AS INT
SET @PageNo = 2SET @PageSize = 2SELECT *
FROM dbo.DemoTable DT
ORDER BY
DT.ID
OFFSET ((@PageNo - 1) * @PageSize)) ROWSFETCH NEXT @PageSize ROWS ONLY
Performance
I did a small test using all the 3 ways and have found the Denali (OFFSET and FETCH) way the best performing one followed by the ROW_NUMBER().
Conclusion
I would prefer using the Denali way just for 2 simple reasons -
- Simplicity of code
- Better performance
Remarks
- The Denali code is based on SQL Server Denali CTP 1 and might change after further releases.
Tuesday, 29 November 2011
Workarounds for SSRS 2008 Font Rendering Issue in ReportViewer 10.0
Problem
A report developed using SSRS 2008 either through Visual Studio 2010 or BIDS 2008 does not renders properly when viewed in the Report Viewer 10.0 via Remote Desktop or any 3rd party applications using Terminal Services in the background.
Remote Rendering
Local Rendering
Workarounds
- Change the Screen Resolution to 1024 X 768 or any 4:3 aspect ratio for both the Remote machine as well as the local machine.
- Export the report as PDF and then take the print outs if required.
- If the above two solutions does not work for you, unfortunately you will have to look back to SSRS 2005.
Saturday, 1 October 2011
How to find out which Table is not having rows in SQL Server?
Introduction
Many times while tuning our production databases we might try to find out the list of tables not having even a single row of data. Today, I am going to show a simple script which could be used to get a list of tables having ZERO rows.
Script
USE DBName --Change this to the DB Name you want to script for.
GODECLARE @TableRowCount TABLE
(
TableName VARCHAR(255), RowCnt INT )
INSERT @TableRowCount
EXEC sp_msForEachTable 'SELECT PARSENAME(''?'', 1),COUNT(*) FROM ?'
SELECT *
FROM @TableRowCount
WHERERowCnt = 0
ORDER BY
RowCnt
Friday, 30 September 2011
How to find a string value in all the string columns of a table/view in SQL Server ?
Introduction
I am sure many times we all might have come across situations where we need to search/find a string value in all the string columns of a given table/view in SQL Server and return the matching rows from that table.
Unfortunately, we do not have any straight forward way to do this till date AFAIK. Hence, the below script can prove to be quite handy in this situation -
USE DBName --Replace with the DB in which the table resides
GO--Declare variables and initialize them
DECLARE @TableSchema AS VARCHAR(50) = 'SchemaName' --Replace this with the name of Schema of the Table/View
DECLARE @TableName AS VARCHAR(50) = 'TableName' --Replace this with the name of the Table/View to search
DECLARE @SearchString AS VARCHAR(50) = 'SearchString' --Replace this with actual SearchString
DECLARE @Qry AS NVARCHAR(MAX)
DECLARE @Columns AS VARCHAR(MAX)
--Prepare the columnsSET @Columns = STUFF((SELECT '+' + CASE WHEN IS_NULLABLE = 'YES' THEN 'ISNULL(' + C.COLUMN_NAME + ','''')' ELSE C.COLUMN_NAME END
FROM INFORMATION_SCHEMA.COLUMNS C
WHERE C.TABLE_SCHEMA = COALESCE(@TableSchema,C.TABLE_SCHEMA)AND C.TABLE_NAME = COALESCE(@TableName,C.TABLE_NAME)
AND C.DATA_TYPE IN ('CHAR','NCHAR','NTEXT','NVARCHAR','TEXT','VARCHAR')
FOR XML PATH('')),1,1,'')
--Prepare the QuerySET @Qry = N' SELECT ' +
' * ' + ' FROM ' + @TableSchema + '.' + @TableName + ' WHERE ' +@Columns + ' LIKE ''%' + @SearchString + '%'''
--Execute the QueryEXEC SP_EXECUTESQL @QryPlease note that the above script works only for the following column types - CHAR,NCHAR,NTEXT,NVARCHAR,TEXT,VARCHAR
Njoy searching….
