Solved

Dynamic subset based on input parameters

Posted on 2006-11-17
12
304 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
12 Comments
 
LVL 29

Assisted Solution

by:Nightman
Nightman earned 100 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
Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

 
LVL 11

Accepted Solution

by:
rw3admin earned 100 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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

732 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