with t as (select * from emp where depno=10)
总结:可以看做将查询出来的语句块表示为一个临时表
select * from t where empno=xxx
union all sum(col1) as colsum from t
在这里就可以直接使用这个"临时表"
;WITH UpdateOrderInfo AS
(
SELECT o.Id FROM [Order] AS o WITH(NOLOCK)
INNER JOIN #TempOrder AS t ON o.Id = t.OrderId
LEFT JOIN OrderItem AS oi WITH(NOLOCK) ON oi.OrderId = o.Id
WHERE EXISTS(SELECT 1 FROM ShipmentItem WITH(NOLOCK) WHERE OrderItemId = oi.Id AND ShipmentItemShippingStatusId <>6)
)
总结下:其实就是把一大堆重复的代码放在这里,使用个别名,后面的代码访问这块的话直接用这个别名就可以了