Click here to Skip to main content
15,921,840 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello friends,

I have two table
1. FWB_SF_Opportunity (Does not contain VCID)<br />
2. FWB_VWSFData (Contains column VCID)


I had made SP with following query:
SQL
SELECT * FROM (
SELECT * FROM FWB_SF_Opportunity B 
left join FWB_VWSFData A ON A.opportunityid=B.id and ReviewMonth=@ReviewMonth
) T
where T.VCID = isnull(@vcid,T.vcid)


The parameter @VCID can take values 5(records whose vcid = 5) or null(gets all records, including VCID whose value is null)

Problem is when @VCID = NULL, the result should contain all records also VCID whose value is null. But the where condition fails in this case, it only gives record whose VCID = 5 and not null.

any solution?

thanks in advance
Posted
Comments
dhage.prashant01 5-Sep-12 0:29am    
I can solve this issue by using IF ELSE condition and repeating the same code.
But i want to handle in same query.
Any solution??

AFAIK, either you have to have a repeating code to achieve it OR you can pass on something else in case of NULL from your code (during query param creation) and handle the known value in SP, like pass -1 when it's NULL. This will make sure your query param always has a valid value leading to query result as per your need.
 
Share this answer
 
Comments
dhage.prashant01 5-Sep-12 1:05am    
I have tried below code and works perfect for me :)
And thanks for your solution too, surely will try to work on it :)
I tried following


VB
SELECT * FROM (
SELECT * FROM FWB_SF_Opportunity B 
left join FWB_VWSFData A ON A.opportunityid=B.id and ReviewMonth=@ReviewMonth
) T
where
((isnull(@VCID,'') = '' AND 1 = 1)
 OR
 (isnull(@VCID,'') <> '' AND T.VCID = @VCID))


And it works fine
Hope guys who face same situation, get help out of it

Happy coding :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900