Dynamic subset based on input parameters

I have the need to select records from an SQL datasource based on the values of the input parameters. For instance, if I pass 3 parameters and I only supply values for 2 of them, I want my stored procedure to select data based on only those two parameters. Here's the source in SQL Server 2000:

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[spGetWOHeaderSubset]
@WONo int,
@StID int,
@StBlk int,
@EndBlk int,
@FrReqDate datetime,
@ToReqDate datetime

AS

DECLARE @SQLString varchar(200)

SET @SQLString = 'SELECT WoNo, WOType, WOStatus, WODtReq, Block, StID FROM dbo.tblWOHeader WHERE '

If (@WONo > 0)
      SET @SQLString = RTRIM(@SQLString) + ' WONo=@WONo';
Else
      BEGIN
      If @StID>0
            SET @SQLString = RTRIM(@SQLString) + ' StID=@StID AND';
      If @StBlk>0
            SET @SQLString = RTRIM(@SQLString) + ' Block>=@StBlk AND';
      If @EndBlk>0
            SET @SQLString = RTRIM(@SQLString) + ' Block<=@EndBlk AND';
      If @FrReqDate<>NULL
            Set @SQLString = RTRIM(@SQLString) + ' WODtReq>=@FrReqDate AND';
      If @ToReqDate<>NULL
            SET @SQLString = RTRIM(@SQLString) + ' WODtReq<=@ToReqDate AND';
                SET @SQLString = LEFT(@SQLString,DATALENGTH(@SQLString)-3); /* dropping the AND from the last statement used */
      END


EXEC (@SQLString);

I am hoping that I won't have to create a stored procedure for every condition. This is my attempt; you may modify it or change it completely. I am looking for the most bandwidth friendly and readable solution.
Thanks in advance for the help!
techconsAsked:
Who is Participating?
 
rw3adminCommented:
ALTER PROCEDURE [dbo].[spGetWOHeaderSubset]
@WONo int,
@StID int,
@StBlk int,
@EndBlk int,
@FrReqDate datetime,
@ToReqDate datetime

AS

DECLARE @SQLString varchar(200)

SET @SQLString = 'SELECT WoNo, WOType, WOStatus, WODtReq, Block, StID FROM dbo.tblWOHeader WHERE '

If (IsNull(@WONo,0) > 0)
     SET @SQLString = RTRIM(@SQLString) + ' WONo='+Cast(@WONo as Varchar)+' And ';
Else
     BEGIN
     If IsNull(@StID,0)>0
          SET @SQLString = @SQLString + ' StID='+Cast(@StID as Varchar)+' AND ';
     If  IsNull(@StBlk,0)>0
          SET @SQLString = @SQLString + ' Block>='+Cast(@StBlk as Varchar)+'  AND ';
     If  IsNull(@EndBlk,0)>0
          SET @SQLString = @SQLString + ' Block<='+Cast(@EndBlk as Varchar)+'  AND ';
     If  IsNull(@FrReqDate,'1900-01-01')<>'1900-01-01'
          Set @SQLString = @SQLString + ' WODtReq>='''+Cast(@FrReqDate as Varchar)+''' AND ';
     If IsNull(@ToReqDate,'1900-01-01')<>'1900-01-01'
          SET @SQLString = @SQLString + ' WODtReq<='''+Cast(@ToReqDate as Varchar)+''' AND ';
     END
     Set  @SQLString=@SQLString+' 1=1'       

EXEC (@SQLString)
0
 
NightmanCTOCommented:
Give this a shot:

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[spGetWOHeaderSubset]
@WONo int,
@StID int,
@StBlk int,
@EndBlk int,
@FrReqDate datetime,
@ToReqDate datetime

AS

DECLARE @SQLString varchar(4000)
DECLARE @Params nvarchar(4000)
SELECT @Params='@WONo int,@StID int,@StBlk int,@EndBlk int,@FrReqDate datetime,@ToReqDate datetime';

SELECT @SQLString = 'SELECT WoNo, WOType, WOStatus, WODtReq, Block, StID FROM dbo.tblWOHeader WHERE 1=1'

If (@WONo > 0)
     SELECT @SQLString = @SQLString + ' AND WONo=@WONo';
Else
     BEGIN
     If @StID>0
          SELECT @SQLString = @SQLString + ' AND StID=@StID';
     If @StBlk>0
          SELECT @SQLString = @SQLString + ' AND Block>=@StBlk';
     If @EndBlk>0
          SELECT @SQLString = @SQLString + ' AND Block<=@EndBlk';
     If @FrReqDate<>NULL
          SELECT @SQLString = @SQLString + ' AND WODtReq>=@FrReqDate';
     If @ToReqDate<>NULL
          SELECT @SQLString = @SQLString + ' AND WODtReq<=@ToReqDate';
     END

exec sp_executesql @SQLString,@Params,@WONo,@StID,@StBlk,@EndBlk,@FrReqDate,@ToReqDate
0
 
techconsAuthor Commented:
Nightman,

I got the folllowing when I execute it:
Procedure expects parameter '@statement' of type 'ntext/nchar/nvarchar'.

I changed SQLString to nvarchar and it does not fail, however the date selection does not work. Any ideas?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
NightmanCTOCommented:
Sorry, I missed that you had declared as varchar not nvarchar (cut and paste is EVIL!)

comment our exec sp_executesql and run SELECT @SQLString instead. Post the results here. Also, post the values of @FrReqDate AND @ToReqDate
0
 
techconsAuthor Commented:
I specified a valid date for both date parameters and selected a specific stid and I get the following:

SELECT WoNo, WOType, WOStatus, WODtReq, Block, StID FROM dbo.tblWOHeader WHERE 1=1 AND StID=@StID

The date fields are defined as datetime fields in the SQL table.  When I don't specify the StID, I get all the records regardless of the date value in the table, which indicates that it's somehow not liking my comparison to NULL to include the date check in the string.
0
 
rw3adminCommented:
I hate this you hit tab and enter and your comment submits,

you had two problems, you were not convering INT and Date to varchar before adding to your @SQLString
0
 
NightmanCTOCommented:
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[spGetWOHeaderSubset]
@WONo int,
@StID int,
@StBlk int,
@EndBlk int,
@FrReqDate datetime,
@ToReqDate datetime

AS

DECLARE @SQLString nvarchar(4000)
DECLARE @Params nvarchar(4000)
SELECT @Params='@WONo int,@StID int,@StBlk int,@EndBlk int,@FrReqDate datetime,@ToReqDate datetime';

SELECT @SQLString = 'SELECT WoNo, WOType, WOStatus, WODtReq, Block, StID FROM dbo.tblWOHeader WHERE 1=1'

If (@WONo > 0)
     SELECT @SQLString = @SQLString + ' AND WONo=@WONo';
Else
     BEGIN
     If @StID>0
          SELECT @SQLString = @SQLString + ' AND StID=@StID';
     If @StBlk>0
          SELECT @SQLString = @SQLString + ' AND Block>=@StBlk';
     If @EndBlk>0
          SELECT @SQLString = @SQLString + ' AND Block<=@EndBlk';
     If @FrReqDate IS NOT NULL NULL
          SELECT @SQLString = @SQLString + ' AND WODtReq>=@FrReqDate';
     If @ToReqDate IS NOT NULL
          SELECT @SQLString = @SQLString + ' AND WODtReq<=@ToReqDate';
     END

exec sp_executesql @SQLString,@Params,@WONo,@StID,@StBlk,@EndBlk,@FrReqDate,@ToReqDate
0
 
NightmanCTOCommented:
rw3admin - you don't have to cast the entire thing as varchar - sp_executesql allows for parameterised statements - makes life easier.

techcons

In my last post, change

     If @FrReqDate IS NOT NULL NULL
to
     If @FrReqDate IS NOT NULL
0
 
rw3adminCommented:
yea third problem... <>NULL will not work for dates, look at my above and example down here

declare @D datetime
if @D<>NULL
Begin
Select 1
End
else
Begin
Select 2
End
0
 
rw3adminCommented:
ok enough mistakes for a day.... I meant to post this
declare @D datetime
select @D=getdate()
if @D<>NULL
Begin
Select 1
End
else
Begin
Select 2
End
.......
sorry ....
0
 
techconsAuthor Commented:
Mind if I split the points, nightman and rw3admin? I appreciate the quick response.
0
 
NightmanCTOCommented:
No problem - just happy to help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.