MSMVPS.COM
The Ultimate Destination for Blogs by Current and Former Microsoft Most Valuable Professionals.

Dynamic SQL Query Revisted

Bilal Haidar [MVP]

Syndication

All Links

Blogs I Read

My Articles

JavaScript & CSS

Date & Time

SQL Server 2000/2005

Articles I Read

ASP.NET Free Controls

MVFP (Most Valuable Forum Posts)

Based on Richard Dudley's comment on my previous post, here is an updated version of the Dynamic SQL Query:

CREATE PROCEDURE [dbo].[GetReport]
(
    @ID INT,
    @Type INT
)
AS

    DECLARE @strQuery NVARCHAR(4000)
    DECLARE @parameterList NVARCHAR(4000)

    SET @strQuery = N'SELECT * FROM Reports WHERE ( (Reports.ID = @RerpotID) AND '

    IF ( @Type=0 )
    BEGIN
        SET @strQuery = @strQuery  + N' (Reports.[In] = 3) )'
    END
    ELse
    IF ( @Type=1 )
    BEGIN
        SET @strQuery = @strQuery  + N' (Reports.[In] = 2) )'
    END
    ELSE
    IF ( @Type=2 )
    BEGIN
        SET @strQuery = @strQuery  + N' (Reports.[In] = 1) )'
    END
    ELSE
        BEGIN
            SET @strQuery = SUBSTRING(@strQuery, 1, (LEN(@strQuery) - 4))
                SET @strQuery = @strQuery + N' )'
            END
    
    SET @parameterList =   N '@ReportID    INT'            
    EXECUTE SP_EXECUTESQL @strQuery, @parameterList, @ReportID = @ID  
GO


Hope its better now.

Regards


Posted Aug 24 2005, 03:13 PM by simple

Add a Comment

(required)  
(optional)
(required)  
Remember Me?


Copyright © is the original authors. Blog site is an independent site not sponsored by Microsoft. The Yoda blog server and the Brianna SQL server would like to thank www.ownwebnow.com and www.exchangedefender.com. They wouldn't be here and broadcasting without the generosity of Vlad Mazek and his companies.

Powered by Community Server (Commercial Edition), by Telligent Systems