Solved

Sql Server dynamic filtering in a Stored procedure

Posted on 2008-10-10
5
212 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 500 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 92

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 92

Expert Comment

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

Author Comment

by:Nyana22
ID: 22686400
:D
0

Featured Post

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to simplify my SQL statement? 14 56
Sql query 107 88
SSIS I need to get the most old file from a folder in the SSIS package. 3 46
Are triggers slow? 7 25
I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

696 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