T-SQL join table only when the table is not empty
问题 I have the following tables with one column (RecordID): TableOne 101 102 103 104 105 106 TableTwo 101 102 103 104 and want to make join between them only when TableTwo is not empty. This could be done with sample IF statement, but in my real situation this would lead to a lot of code duplication. I have try the following: SELECT * FROM TableOne T1 WHERE exists (select 1 from TableTwo where T1.RecordID=RecordID) and exists (select 1 from TableTwo) using this answer , but the same logic is not