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'的数据并插入它.. –