Sql 基于列值动态确定联接表

Sql 基于列值动态确定联接表,sql,sql-server,dynamic,inner-join,Sql,Sql Server,Dynamic,Inner Join,我想动态地内部连接表,下面是我的SQL查询 Update temp Set temp.Order_Id = parent.ID from #TempTransactions AS temp Inner Join (case when temp.OrderType = 1 then preorders else orders end) AS parent ON parent.Cloud_Id = temp.Order_Id 我是否有可能以上述方式或其他方式做出决定 如果是,怎么做?两个左

我想动态地内部连接表,下面是我的SQL查询

Update temp
Set temp.Order_Id = parent.ID

from #TempTransactions AS temp

Inner Join (case when temp.OrderType = 1 then preorders else orders end)  AS parent

ON parent.Cloud_Id = temp.Order_Id
我是否有可能以上述方式或其他方式做出决定


如果是,怎么做?

两个左连接就可以了

Update temp Set temp.Order_Id = COALESCE(p.ID, o.ID)

from #TempTransactions AS temp

LEFT Join preorders p ON p.Cloud_Id = temp.Order_Id AND temp.OrderType=1
LEFT JOIN orders o ON o.Cloud_Id = temp.Order_Id AND (temp.OrderType <> 1 OR temp.OrderType IS NULL)
WHERE COALESCE(p.ID, o.ID) IS NOT NULL
Update temp Set temp.Order\u Id=合并(p.Id,o.Id)
从#将交易作为临时交易
左连接p.Cloud上的预订单p\u Id=temp.Order\u Id和temp.OrderType=1
o.Cloud\u Id=temp.Order\u Id上的左连接订单o和(temp.OrderType 1或temp.OrderType为NULL)
其中COALESCE(p.ID,o.ID)不为空

两个左连接就可以了

Update temp Set temp.Order_Id = COALESCE(p.ID, o.ID)

from #TempTransactions AS temp

LEFT Join preorders p ON p.Cloud_Id = temp.Order_Id AND temp.OrderType=1
LEFT JOIN orders o ON o.Cloud_Id = temp.Order_Id AND (temp.OrderType <> 1 OR temp.OrderType IS NULL)
WHERE COALESCE(p.ID, o.ID) IS NOT NULL
Update temp Set temp.Order\u Id=合并(p.Id,o.Id)
从#将交易作为临时交易
左连接p.Cloud上的预订单p\u Id=temp.Order\u Id和temp.OrderType=1
o.Cloud\u Id=temp.Order\u Id上的左连接订单o和(temp.OrderType 1或temp.OrderType为NULL)
其中COALESCE(p.ID,o.ID)不为空

这太棒了:)这太棒了:)