【发布时间】:2014-02-12 14:59:52
【问题描述】:
有没有办法让 if 语句确定是否需要连接?
我需要这个,因为我查询的数据中有多个外键。如果任何外键为空,我需要更改 SQL 语句。我想写一个可以识别任何空值的语句。
这就是我想做的……
select a.*,b.* from table1 a inner join table2 b on a.id = b.id
if a.InspectorID is not null
{inner join table3 c on a.InspectionID = c.id}
else do nothing...
【问题讨论】:
-
你不能用
left join吗?还是我错过了重点? -
如果不使用动态 SQL,您将无法更改查询的 形状(使用的表、列数、结果集中列的名称和类型)。但在这种情况下,有什么意义 - 你从不使用来自
c的任何列? -
我想我没有完全理解左连接...christiandev 谢谢
标签: sql-server tsql inner-join