1

i've been searching SO for a while and found nothing related to this.

We use heavily the dynamic approach for most of our queries like this:

Declare @ContactId VarChar(8000)

Select @ContactId = '1'

Select * 
From Person.Contact
Where 1 = 1 And 
Case When Len(Ltrim(Rtrim(@ContactId))) = 0 Then 1 Else ContactID End =
Case When Len(Ltrim(Rtrim(@ContactId))) = 0 Then 1 Else @ContactId End

This way the query gets filtered dynamically if there's a value on the parameter

But the problem comes when trying the same stuff with several IDs like so:

Declare @ContactId VarChar(8000)

Select @ContactId = '1,2,3'

Select * 
From Person.Contact
Where 1 = 1 And 
Case When Len(Ltrim(Rtrim(@ContactId))) = 0 Then 1 Else ContactID End In (
Select Case When Len(Ltrim(Rtrim(@ContactId))) = 0 Then 1 Else ( Select id From dbo.SplitString(@ContactId,',') ) End )

Sql throws an error "Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression."

that's totally normal and expected, my question is:

Is there a way to dynamically do this kind of filtering ?

The solution we use is this:

Declare @Table Table(
    Col1    Int,
    Col2    Int,
    Col3    Int
)

Insert @Table
Select Col1, Col2, Col3
From Person.Contact
Where [many filters except the in clause filters]

If Len(Ltrim(RTrim(@ContactId))) > 0
    Select * 
    From @Table
    Where ContactId In ( Select Id From dbo.SplitString(@ContactId, ',') )
Else 
Select *
From @Table

But it's not an option when the query is massive and feeding a table variable is overkill for this. Hope i made my point and someone is kind enough to help me find a solution to this.

PS: Using sp_ExecuteSql is not an option in this scenario either, sorry.

1
  • If you rule out the two ways that you would do something, then yes, you're going to have a hard time. Commented Oct 18, 2012 at 15:38

2 Answers 2

1

How you should do this is with a table parameter.

But if you want to persist in this approach.

Declare @ContactId VarChar(8000) 

Select @ContactId = '1,2,3'
Select *  
From Person.Contact 
where ',' + @contactID + ',' like '%,'+convert(varchar(50),contactid)+',%'
Sign up to request clarification or add additional context in comments.

6 Comments

This approach is interesting enough, but the sad part is that it's not selecting anything if the parameter is empty :(
@SamCcp add an or len(@contactID)=0
Ok, now we're getting somewhere, by adding that condition, it does bring the desired data even when the parameter is empty, but i can't play with many conditions like this right? I mean how could i search for another condition say... @EmailPromotion = '1,2'
@SamCcp Sure you can. Stick ( around the like / or ) and add as many other filters as you like. eg: and (','+@emailpromotion+',' like '%,' + emailpromotion + ',%')
Yeah, i just figured it out, thanks, i'll run some queries and test the performance, in the meantime, you sir earned a fan. Thanks.
|
1

Try this

Select * 
From Person.Contact
Where (
           len(@contactid) > 0 AND 
           ContactID IN (Select id From dbo.SplitString(@ContactId,','))) 
or 1 = 1

2 Comments

Same stuff than previous answer, it doesn't display anything if the parameter is empty and the OR part must be omitted because it brings all the rows on the table.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.