Consider a simple join:
Select TableB.*
From TableA
Inner Join TableB
On TableB.ID = TableA.ID
What I want to do is decide which table to join to depending on a parameter. Although the following syntax is not valid, I wrote it just to illustrate what I am after:
Select TableD.*
From TableA
Inner Join
[If @useTableC = 1 Then Join to TableC Else Join to TableB] As TableD
Both TableB and TableC have identical columns.
How can I create this kind of join. Please be aware that this example is actually a small portion of a much larger query so you cannot just use If…Else statements.
Thanks a lot!
Usually I’d prefer to rewrite in more than one statement, but if you really need it: