我有一个声明的表,其中包含大量数据...我通过在InventoryLogs
上执行选择查询来获取数据..现在,我想要的是将此数据插入另一个表叫MonthlySalesHistoryDetail
...但是我不知道如何获取我声明的表的值......
这是一个存储过程:
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
提前感谢....
答案 0 :(得分:0)
我不知道有可能做这个插入......我认为它只对declared table
INSERT INTO MonthlySalesHistoryDetails
(
MonthlySalesHistoryID,
ItemID,
MeasurementUnitID,
QuantitySold,
QuantityReturned,
TotalSoldAmount,
TotalReturnedAmount
)
SELECT
d.MonthlySalesHistoryID,
d.ItemID,
d.MeasurementUnitID,
d.QuantitySold,
d.QuantityReturned,
d.TotalSoldAmount,
d.TotalReturnedAmount
FROM @details d Where d.RowID = @counter