0
我在select語句中獲取了id的列表。如何通過每個ID循環並打印下面如何遍歷ID列表
DECLARE @InventoryID INT
Select @InventoryID = I.InventoryID
From G2owner.LoadSlip L (nolock)
Inner Join G2owner.LoadSlipReleaseOrderMapping LM (nolock) on L.LoadSlipID = LM.LoadSlipID
Inner Join G2owner.LoadSlipDetail LSD (nolock) on LM.LoadSlipReleaseOrdermappingID = LSD.LoadSlipReleaseOrdermappingID
Inner Join G2owner.Inventory I (nolock) on LSD.InventoryID = I.InventoryID
Where LM.ReleaseOrderID = 7156 and L.LoadSlipID = 3014
and L.TerminalID = 3 and I.InventoryEndDate IS NULL and I.TotalNetWeight = 0.0000000
PRINT @InventoryID
是什麼讓你覺得你需要一個循環?只需使用'Select I.InventoryID From ...'。 –
我需要循環,因爲我需要在其他存儲過程中的多個位置使用每個invetoryID – user1030181
即使將ID列表轉儲到臨時表,您也應儘量嘗試儘量使用基於集合的邏輯。但是,如果你堅持要使用循環,你必須[使用遊標](http://stackoverflow.com/questions/11856518/sql-server-2008-request-example-on-creating-a -cursor-to-loop-through-records)來執行你所描述的內容。 –