?
Solved

Sql Server dynamic filtering in a Stored procedure

Posted on 2008-10-10
5
Medium Priority
?
218 Views
Last Modified: 2012-05-05
Hello,
I am writing the following stored procedure, i want depending on the input parameter to set the filter expression of the where clause,
But i am getting errors, can someone help please?
ALTER PROCEDURE [dbo].[interConfParam] 
(
 
@PType nvarchar(20)
)
 
AS
BEGIN
DECLARE @ProductType nvarchar(50)
SELECT @ProductType = CASE @PType WHEN 'ALL' THEN 'IN ('Body', 'Plug')' ELSE @Ptype END 
SELECT     S2ID, ProductType
FROM         SrDDW
WHERE     (ProductType = @ProductType)
 
END

Open in new window

0
Comment
Question by:Nyana22
[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 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 22686333
this will do:
ALTER PROCEDURE [dbo].[interConfParam] 
(
 
@PType nvarchar(20)
)
 
AS
BEGIN 
SELECT     S2ID, ProductType
FROM         SrDDW
WHERE   (   ( @PType = 'ALL' AND ProductType IN ('Body', 'Plug'))
         OR ( ProductType =  @PType )
        )

Open in new window

0
 
LVL 60

Expert Comment

by:chapmandew
ID: 22686348
ALTER PROCEDURE [dbo].[interConfParam]
(
 
@PType nvarchar(20)
)
 
AS
BEGIN
DECLARE @sql nvarchar(4000)
DECLARE @ProductType nvarchar(50)
SELECT @ProductType = CASE @PType WHEN 'ALL' THEN 'IN ('Body', 'Plug')' ELSE @Ptype END
SET @sql = 'SELECT     S2ID, ProductType
FROM         SrDDW
WHERE     (ProductType IN(' + CASE @Ptype When 'ALL' THEN '''Body'', ''Plug''' ELSE @PType END + ')'
exec sp_executesql @sql
END
 
0
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 22686377
ALTER PROCEDURE [dbo].[interConfParam]
(
 
@PType nvarchar(20)
)
 
AS
BEGIN
DECLARE @ProductType nvarchar(50), @sql varchar(max)

SET @sql = 'SELECT S2ID, ProductType FROM SrDDW WHERE ProductType '

IF @PType = 'ALL'
    SET @sql = @sql + 'IN (''Body'', ''Plug'')'
ELSE
    SET @sql = @sql + '= ''' + @Ptype + ''''
END

EXEC(@sql)
 
END
0
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 22686382
Guess I need more coffee :)
0
 

Author Comment

by:Nyana22
ID: 22686400
:D
0

Featured Post

What Is Blockchain Technology?

Blockchain is a technology that underpins the success of Bitcoin and other digital currencies, but it has uses far beyond finance. Learn how blockchain works and why it is proving disruptive to other areas of IT.

Question has a verified solution.

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

If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

764 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