我想从一列插入多行。
我的POS系统是这样的
<表类>
ID
项目代码总数量数量的托盘 一个托盘的数量/每箱 日期 tbody><<tr>00001 第1项 384 4 96 12/07/2014 表类>
你的问题需要进一步澄清
但是总的来说,您需要使用INSERT INTO SELECT
命令
你可以通过搜索找到更多关于它的信息,但是这里有一个适合你的案例的快速代码
重复SELECT
语句,按照您希望在第二个表中复制该行数据的次数:
INSERT INTO table1 (
ID,
ItemCode,
QtyInBox,
Date
)
(
SELECT ID, ItemCode, QtyInBox, Date FROM table2 WHERE ID = 0001
SELECT ID, ItemCode, QtyInBox, Date FROM table2 WHERE ID = 0001
SELECT ID, ItemCode, QtyInBox, Date FROM table2 WHERE ID = 0001
SELECT ID, ItemCode, QtyInBox, Date FROM table2 WHERE ID = 0001
)
根据标题,这是你问题的答案,你只需要考虑托盘东西,你可以走了。
可以这样使用递归CTE:
WITH CTE AS
(
SELECT ID, ItemCode, QuantityOfPalleTPerBox as [Qty in Box], [Date],
1 AS cn, QtyOfPallets
FROM table_name
UNION ALL
SELECT ID, ItemCode, [Qty in Box], [Date],
cn+1, QtyOfPallets
FROM CTE WHERE cn+1<=QtyOfPallets
)
SELECT ID, ItemCode, [Qty in Box], [Date] FROM CTE
ORDER BY ID
查看演示
use this:
INSERT INTO [table2] ([ID], [Item_Code], [Qty_in_Box], [Date])
(
SELECT [ID], [Item_Code], [Quantity_Of_A_PalleT/Per_Box], [Date] FROM [table1]
UNION ALL
SELECT [ID], [Item_Code], [Quantity_Of_A_PalleT/Per_Box], [Date] FROM [table1]
UNION ALL
SELECT [ID], [Item_Code], [Quantity_Of_A_PalleT/Per_Box], [Date] FROM [table1]
UNION ALL
SELECT [ID], [Item_Code], [Quantity_Of_A_PalleT/Per_Box], [Date] FROM [table1]
)