SQLUSA.com
SQL SERVER 2008 GRAND SLAM
FREE TRIAL  CLICK HERE TO ORDER  SEARCH
SQL JOBS SQL Server Training Scripts JOB BANK
  SQL Server 2008 Training Scripts  
  SQL Server 2005 Training Scripts  
News SQL Server Articles SQL Format

How to design a stored procedure with dynamic SQL filtering?

Execute the following Microsoft SQL Server 2008 T-SQL database scripts in SSMS Query Editor to demonstrate the use of dynamic SQL to construct a stored procedure (sproc) with dynamic filtering for web page support. Based on internet user choices (or lack of choices), specialized query is built in a string and dynamically executed.

USE AdventureWorks2008;

GO

 

CREATE /* ALTER */ PROCEDURE procWebProductSearch

(

    @ProductSubcategoryID INT,

    @Color NVARCHAR(15),

    @ListPrice INT

)

 

AS

SET NOCOUNT ON

DECLARE @SQLCommand NVARCHAR(MAX)

 

 

SET @SQLCommand = ' SELECT ProductName=Name, * FROM Production.Product '

 

-- Check if WHERE clause filtering needed

IF @ProductSubcategoryID IS NOT NULL

    OR @Color IS NOT NULL

    OR @ListPrice IS NOT NULL

BEGIN

    SET @SQLCommand = @SQLCommand + ' WHERE '

END

 

 

IF @ProductSubcategoryID IS NOT NULL BEGIN

    SET @SQLCommand = @SQLCommand + ' ProductSubcategoryID = '

        + convert(varchar,@ProductSubcategoryID) + ' '

END

 

 

IF @Color IS NOT NULL BEGIN

    IF @ProductSubcategoryID IS NOT NULL

        SET @SQLCommand = @SQLCommand + ' AND '

    SET @SQLCommand = @SQLCommand + ' Color LIKE '''  +

                      @Color + '%'''

END

 

 

IF @ListPrice IS NOT NULL BEGIN

    IF @ProductSubcategoryID IS NOT NULL

    OR @Color IS NOT NULL

        SET @SQLCommand = @SQLCommand + ' AND '

    SET @SQLCommand = @SQLCommand + ' ListPrice <= ' +

        CAST(@ListPrice AS VARCHAR)

END

 

-- Final sort

SET @SQLCommand = @SQLCommand + ' ORDER BY ProductName '

 

-- Debugging statement

PRINT @SQLCommand

 

-- Execute the dynamically constructed SQL query

EXEC sp_executeSQL @SQLCommand

GO

 

 

-- Test scripts

 

-- Web site user selects Subcategory only from a drop-down menu

-- ProductSubcategoryID 20 is Full-Finger Gloves, L

-- Tab over to Messages to see the dynamically created SQL queries

 

EXEC procWebProductSearch 20, null, null

GO

 

-- Web site user also specifies highest price

EXEC procWebProductSearch 20, null, 30

GO

 

-- Color is also specified by radio-button selection on the website

EXEC procWebProductSearch 20, 'Red', 30

GO

 

EXEC procWebProductSearch 20, 'Black', 30

GO

 

 

Order SQL 2008 GRAND SLAM Today!
SQLUSA.com Home Page
SQL Server Training at www.sqlusa.com.
SQL Server 2008 Video Training at www.sqlusa.com.
SQL Server 2005 Training Videos at www.sqlusa.com.
Microsoft SQL Server 2000 Training Videos at www.sqlusa.com.

FREE SQL & Business Intelligence / OLAP Short Videos on YOUTUBE.com

Microsoft Community Contributor 2011
Invest in Your SUCCESS!

Search SQLUSA FREE SQL Server Articles & FREE T-SQL Scripts


Copyright 2005-2011, SMI Corp. All Rights Reserved.

SQL Server 2012 is a program product of Microsoft Corporation.
SQL Server 2008 is a program product of Microsoft Corporation.
SQL Server 2005 is a program product of Microsoft Corporation.
SQL Server 2000 is a program product of Microsoft Corporation.