Author |
Topic |
Netjunkie
Starting Member
17 Posts |
Posted - 2012-03-21 : 13:23:22
|
Hello Everyone,I need some help with optional comma separated value to be passed to a parameter.CREATE PROCEDURE dbo.usp_XXXXXXXXX_Search @distance int, @first_name varchar(35) = NULL, @last_name varchar(35)= NULL, @OPTIONAL_CSV_PARAMETER VARCHAR(30) = NULL ASBEGIN (SELECT TOP 100 ABC, XYZ, ... etcfrom Table1 INNER JOIN Table2 ON Table1.ID = Table2.IDLEFT JOIN TABLE3 on Table2.ID = Table3.ID and Table3.ID in (SELECT * FROM dbo.fn_Split( @OPTIONAL_CSV_PARAMETER ,',')))Now if the parameter is passed with a value like '10, 20, 30', this works fine.But if the parameter is NULL, then it doesn't return anything. which is not the expected result if the parameter is optional.Any thoughts on how these kind of scenarios should be handled?I do not want to go with Dynamic SQL for performance and security reasons.Please help me with your suggestions. |
|
X002548
Not Just a Number
15586 Posts |
|
Netjunkie
Starting Member
17 Posts |
Posted - 2012-03-21 : 13:46:37
|
I apologize if i wasnt clear.. I mean, it doesn't return any results when it should have |
 |
|
X002548
Not Just a Number
15586 Posts |
|
Netjunkie
Starting Member
17 Posts |
Posted - 2012-03-21 : 14:01:32
|
Brett,I had considered this as an option but the code block is ~ 400 lines and it will have to be written twice just for this optional parameter.Any logic changes will have to be done in both code blocks.So i was checking to see if there is any other better approach. |
 |
|
X002548
Not Just a Number
15586 Posts |
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2012-03-21 : 19:55:21
|
It probably won't perform well, but you could change the join condition a little:LEFT JOIN TABLE3 on Table2.ID = Table3.ID and ( Table3.ID in (SELECT * FROM dbo.fn_Split( @OPTIONAL_CSV_PARAMETER ,',')) OR @OPTIONAL_CSV_PARAMETER IS NULL ) |
 |
|
Netjunkie
Starting Member
17 Posts |
Posted - 2012-03-26 : 05:41:05
|
Performance took a serious hit with the above approach.I guess the only simple option will be to write 2 separate code blocks to execute based on IF @OPTIONAL_CSV_PARAMETER is nullTHEN CODEBLOCK WITHOUT IN CLAUSEELSE CODEBLOCK WITH IN CLAUSE |
 |
|
|