Solved

Dynamic subset based on input parameters

Posted on 2006-11-17
12
288 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 100 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 11

Accepted Solution

by:
rw3admin earned 100 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 29

Expert Comment

by:Nightman
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Mind if I split the points, nightman and rw3admin? I appreciate the quick response.
0
 
LVL 29

Expert Comment

by:Nightman
Comment Utility
No problem - just happy to help.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now