Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

INNER JOIN only if condition is met

Status
Not open for further replies.

jasonhuibers

Programmer
Sep 12, 2005
290
CA
I want to INNER JOIN a table only if a parameter condition is met. I am trying to do something like below. How can I do this?


IF @EnteredDate >= CONVERT(CHAR, GetDate(), 101)
INNER JOIN Names WITH (NOLOCK) ON Emp.Code = Names.Code
ELSE
INNER JOIN tmp_Names WITH (NOLOCK) ON EMP.Code = Names.Code
 
Borisslav recently answered similar question on another forum

(but in that case it was easier, since the same table was used - different fields)

ON TABLE1.ID_FLD = CASE WHEN @EnteredDate >= CONVERT(CHAR, GetDate(), 101) >=
THEN TABLE2.ID_FLD
ELSE TABLE3.ID_FLD END

In your case I suggest to branch your logic to two separate paths.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top