TSQL - Filter/Show All or specific records from Select query

-- Trick to filter/show all or specific records from select query 
DECLARE @AllRecords NCHAR(1) 

IF (@RoleID <= 0) 
SET @AllRecords = N'Y' 
ELSE 
SET @AllRecords = N'N' 

SELECT 
ROW_NUMBER() OVER (ORDER BY ar.RoleId) AS RowID , 
RoleName, 
.... 

FROM 
table1 
... 
.. 
. 
WHERE 
ar.RoleID = @RoleID OR @AllRecords = N'Y' 
... 
.. 
. 

Comments

Popular posts from this blog

ASP.NET Compillation Error BC31007 - Solution

The Difference between GET and POST

Test & Debug WCF service using WCFTestClient.exe