Showing posts with label Sql Server 2008 R2. Show all posts
Showing posts with label Sql Server 2008 R2. Show all posts

Friday, November 18, 2011

Parameterize Sorting Stored Procedure


In Data-driven application or website, dynamic sorting is common need. Ideally we should write stored procedure for dynamic sorting.

We found nice solution for above problem. Bellow stored procedure fetch ordered data with passed parameter. There two parameters for dynamic sorting stored procedure. Parameter @sortDirection pass for order direction (asc or desc). Second parameter @sortCol for pass sorting field name.


Code


=====================================================-
-- Create date:   18-Nov-2011
-- Description:   Example of Fetch Data With Sorting Parameter
=====================================================
Create PROCEDURE Product_Sorting_Parameter
    -- Add the parameters for the stored procedure here
    @sortDirection as varchar(5),
    @sortCol as varchar(50)
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from

    SET NOCOUNT ON;
    -- Select Query
    SELECT
        [ProductID] ,[Name],[ProductNumber],[Color],[ListPrice],[Size]     
     FROM         [AdventureWorks2008R2].[Production].[Product]
     ORDER BY
     -- Name
     Case WHEN @sortCol = 'Name' and @sortDirection = 'asc' THEN Name end,
     Case WHEN @sortCol = 'Name' and @sortDirection = 'desc' THEN Name end desc,
     -- Size
     Case WHEN @sortCol = 'Size' and @sortDirection = 'asc' THEN Size end,
     Case WHEN @sortCol = 'Size' and @sortDirection = 'desc' THEN Size end desc,
     -- Color
     Case WHEN @sortCol = 'Color' and @sortDirection = 'asc' THEN Color end,
     Case WHEN @sortCol = 'Color' and @sortDirection = 'desc' THEN Color end desc,
     -- Price
     Case WHEN @sortCol = 'Price' and @sortDirection = 'asc' THEN ListPrice end,
     Case WHEN @sortCol = 'Price' and @sortDirection = 'desc' THEN ListPrice end desc
END
GO

Sunday, June 19, 2011

NULLIF (Transact-SQL)

Yesterday I found one more function of sql server. NULLIF which returns the first expression if the two expressions are not equal. If the expressions are equal, NULLIF returns a null value of the type of the first expression. NULLIF function is available from Sql Server 2005.

Syntax :

NULLIF ( expression , expression )

Example :

The following example creates a budgets table to show a department (dept) its current budget (current_year) and its previous budget (previous_year). For the current year, NULL is used for departments with budgets that have not changed from the previous year, and 0 is used for budgets that have not yet been determined. To find out the average of only those departments that receive a budget and to include the budget value from the previous year (use the previous_year value, where the current_year is NULL), combine the NULLIF and COALESCE functions. 

ISNULLIF Example

Friday, June 17, 2011

History Of MS SQL Server

SQL Server 2008 R2 is the latest version of a database server product that has been evolving since the late 1980s. Microsoft SQL Server originated as Sybase SQL Server in 1987. 

In 1988, Microsoft, Sybase, and Aston-Tate ported the product to OS/2. Later, Aston-Tate dropped out of the SQL Server development picture, and Microsoft and Sybase signed a co-development agreement to port SQL Server to Windows NT. The co-development effort cumulated in the release of SQL Server 4.0 for Windows NT. After the 4.0 release, Microsoft and Sybase split on the development of SQL Server; Microsoft continued forward with future releases targeted for the Windows NT platform while Sybase moved ahead with releases targeted for the UNIX platform, which they still market today.