?
Solved

Dynamic subset based on input parameters

Posted on 2006-11-17
12
Medium Priority
?
312 Views
Last Modified: 2008-03-06
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!
0
Comment
Question by:techcons
  • 5
  • 4
  • 3
12 Comments
 
LVL 29

Assisted Solution

by:Nightman
Nightman earned 400 total points
ID: 17968200
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
 

Author Comment

by:techcons
ID: 17968290
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
 
LVL 29

Expert Comment

by:Nightman
ID: 17968317
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Accepted Solution

by:
rw3admin earned 400 total points
ID: 17968369
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
 

Author Comment

by:techcons
ID: 17968379
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
 
LVL 11

Expert Comment

by:rw3admin
ID: 17968401
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
 
LVL 29

Expert Comment

by:Nightman
ID: 17968411
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
 
LVL 29

Expert Comment

by:Nightman
ID: 17968425
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
 
LVL 11

Expert Comment

by:rw3admin
ID: 17968428
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
 
LVL 11

Expert Comment

by:rw3admin
ID: 17968434
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
 

Author Comment

by:techcons
ID: 17968477
Mind if I split the points, nightman and rw3admin? I appreciate the quick response.
0
 
LVL 29

Expert Comment

by:Nightman
ID: 17968509
No problem - just happy to help.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have a large data set and a SSIS package. How can I load this file in multi threading?
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question