Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Sql Server dynamic filtering in a Stored procedure

Posted on 2008-10-10
5
Medium Priority
?
222 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

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…
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

604 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