cancel
Showing results for 
Search instead for 
Did you mean: 

SQL query (conditional join)

richard_pietsch
Active Contributor
0 Kudos
259

Hi experts,

I would like to create a query that combines two (sub-)sql queries depending on their result.
The first one returns a single value, lets say valtxt = 'A', but also can be null (if the record does not exist fot the ).

SELECT valkey FROM mxi_AttrValueHelp WITH(NOLOCK) WHERE valkey IN (SELECT avalue FROM idmv_value_basic WITH(NOLOCK) WHERE attrname = 'MX_USERTYPE' AND mskey = ?? ) 
The second sql returns all possible values (e.g. 'A', 'B', 'C',..).
SELECT valkey FROM mxi_AttrValueHelp WITH(NOLOCK) WHERE valid = 'MX_USERTYPE'


So, if the first sql returns no value, the complete list from second sql should be returned otherwise only the result of first sql. I've played with JOINs, ISNULL, EXISTS and also UNION.. but I don't get it working.. >_<
Please help

Regards, Richard

Accepted Solutions (0)

Answers (0)