.net - Use IF to identify what inner join should i used -


i have stored procedure this

select name, identifier, referenceid, referenceaccountid test 

and before start inner join, have field named identifier. if 1 use inner join ref_reference1 else use other one.

inner join ref_reference1 on ref_reference1.referenceid = test.referenceid inner join ref_reference2 on ref_reference2.referenceid = test.referenceid 

how can solve this?

you don't. cannot change "shape" of query without using dynamic sql, , should avoid possible.

instead, can 2 left joins:

left join ref_reference1 on      ref_reference1.referenceid = test.referenceid      , test.identifier = 1 left join ref_reference2 on      ref_reference2.referenceid = test.referenceid      , test.identifier <> 1 

you'll notice have strong benefit - can reference these tables elsewhere in query (how planning deal in proposal?)


Comments