0
我有一個聲明的表,其中有很多數據...我通過在我的InventoryLogs
上執行選擇查詢來獲取數據..現在,我想要的是將此數據插入另一張表稱爲MonthlySalesHistoryDetail
......不過我不知道如何讓我的申報表的值...使用聲明表的SQL循環插入
這是一個存儲過程:
Alter Procedure InsertMonthlySalesHistoryEndDate
@CurrentDate date,
@CreatedByID int,
@LastInsertID int
as
Declare @details table
(
RowID int identity(1,1) primary key,
MonthlySalesHistoryID int,
ItemID int,
MeasurementUnitID int,
QuantitySold numeric(18,4),
QuantityReturned numeric(18,4),
TotalSoldAmount numeric(18,4),
TotalReturnedAmount numeric(18,4)
)
Insert Into @details
(
MonthlySalesHistoryID,
ItemID,
MeasurementUnitID,
QuantitySold,
QuantityReturned,
TotalSoldAmount,
TotalReturnedAmount
)
SELECT
@LastInsertID,
il.ItemID,
il.MeasurementUnitID,
SUM(il.Quantity) as QuantitySold,
ISNULL((SELECT SUM(Quantity) FROM InventoryLogs WHERE TransactionType = 15 AND CAST(InventoryLogDate as date) = @CurrentDate),0) as QuantityReturned,
SUM(il.ComputedCost) as TotalSoldAmount,
ISNULL((SELECT SUM(ComputedCost) FROM InventoryLogs WHERE TransactionType = 15 AND CAST(InventoryLogDate as date) = @CurrentDate),0) as TotalReturnedAmount
FROM InventoryLogs il
WHERE il.TransactionType = 9 AND CAST(InventoryLogDate as date) = @CurrentDate
GROUP BY il.ItemID, il.MeasurementUnitID
declare @count int = (SELECT COUNT(*) FROM @details)
declare @counter int = 0
WHILE(@count > @counter)
BEGIN
SET @counter = @counter + 1
SELECT * FROM @details d Where d.RowID = @counter
INSERT INTO MonthlySalesHistoryDetails
(
MonthlySalesHistoryID,
ItemID,
MeasurementUnitID,
QuantitySold,
QuantityReturned,
TotalSoldAmount,
TotalReturnedAmount
)
VALUES
(
//I want to get the values of my
//SELECT * FROM @details d Where d.RowID = @counter here..
)
END
在此先感謝....
我已經知道了....我的壞..我不知道這是可能的... –
下一個問題:爲什麼'@ details'仍然存在?插入...選擇 –
@IvanStarostin你是什麼意思? 'insert'會得到'select'的數據並插入它.. –