Aug 18 Chakkaradeep | .NET

Optional Parameters in SQL Server Stored Procedures

Today I had the need to have optional parameters in my SQL stored procedure. And It wasn't that complex to implement :)

Below is the code:

ALTER PROCEDURE [dbo].[GetSampleRequests]
    @Org varchar(50),
    @Type varchar(50),
    @From DateTime,
    @To DateTime
AS
BEGIN
 
SELECT 
    Organisation,
    ServiceType,
    COUNT(DataStatus) AS DataStatusCount
FROM
    SampleLog
WHERE
    ((@From IS NULL OR @To IS NULL) OR
         TransactionDateTime BETWEEN @From and @To) AND 
    (@Org IS NULL OR Organisation = @Org) AND 
    (@Type IS NULL OR ServiceType = @Type)
GROUP BY
    Organisation,
    ServiceType
ORDER BY
    Organisation,
    ServiceType
END

 

The magic happens when we check whether the parameter is IS NULL :)

Aug 16 Chakkaradeep | .NET

.NET CrossTabs

Recently I had the necessity to do cross-tab report with the SQL Server data. First thin that comes into everybody's mind is to use the PIVOT functionality in SQL Server. But, being a programmer, I am not that fluent in writing and managing complex queries. I was looking out for some .NET implementation and found an excellent article here which helped me accomplish this task :)

I think this is a lot easier for me than writing SQL queries ;)

And here is performance comparison of .NET CrossTabs and SQL Server CrossTabs


Creative Commons License
Chaks' Corner Blog by Chakkaradeep Chandran is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.
Based on a work at www.chakkaradeep.com.
Permissions beyond the scope of this license may be available at http://www.chakkaradeep.com.