我对SQL有疑问。
SELECT TOP (10) ordr.ordID
FROM [inSight].[dbo].[Orders] ordr
INNER JOIN [inSight].[dbo].[OrderLines] ordn ON ordr.ordID = ordn.ordID
JOIN [inSight].[dbo].[OrderLineAttributeValues] atr ON ordn.olnID = atr.olnID
WHERE
atr.atbID=190 AND
atr.olnavValue=1
(以上查询返回多个结果)
所以我想做的是,从上面的查询中获取结果并逐一在下面的查询中使用它。
SELECT op.optCode
FROM [inSight].[dbo].[Orders] ordr
INNER JOIN [inSight].[dbo].[OrderLines] ordn ON ordr.ordID = ordn.ordID
JOIN [inSight].[dbo].[OrderLineOptions] ordlo ON ordn.olnID = ordlo.olnID
JOIN [inSight].[dbo].[Features] ftr ON ordlo.ftrID = ftr.ftrID
JOIN [inSight].[dbo].[Options] op ON ftr.ftrID = op.ftrID
WHERE
ordr.ordID = @OrderNumber AND
ftr.ftrID = 1477 AND
ordlo.optid = op.optID
上面的变量OrderNumber是第一个查询的结果。
我不想使用游标,有没有其他方法可以解决这个问题?
答案 0 :(得分:1)
你看过交叉申请吗?
这可能对您有用:
SELECT op.optCode
FROM [inSight].[dbo].[Orders] ordr
INNER JOIN [inSight].[dbo].[OrderLines] ordn ON ordr.ordID = ordn.ordID
JOIN [inSight].[dbo].[OrderLineOptions] ordlo ON ordn.olnID = ordlo.olnID
JOIN [inSight].[dbo].[Features] ftr ON ordlo.ftrID = ftr.ftrID
JOIN [inSight].[dbo].[Options] op ON ftr.ftrID = op.ftrID
CROSS APPLY (
SELECT TOP (10) ordr.ordID
FROM [inSight].[dbo].[Orders] ordr
INNER JOIN [inSight].[dbo].[OrderLines] ordn ON ordr.ordID = ordn.ordID
JOIN [inSight].[dbo].[OrderLineAttributeValues] atr ON ordn.olnID = atr.olnID
WHERE
atr.atbID=190 AND
atr.olnavValue=1 AND
ordr.ordID = op.optCode
) ca
WHERE
ftr.ftrID = 1477 AND
ordlo.optid = op.optID