简体   繁体   English

根据另一个表中的列值多次INSERT行

[英]INSERT rows multiple times based on a column value from another table

Mainly, I would like to insert a row in table 1 multiple times, based on an integer value in a column of table 2. 主要是,我想基于表2的列中的整数值多次在表1中插入一行。

My situation 我的情况

Table 2 contains a column 'SKU' and 'stock', and I would like to insert the 'SKU' and a timestamp into table 1. I want this row duplicated for 'stock'-value times in table 1. 表2包含“SKU”和“stock”列,我想在表1中插入“SKU”和时间戳。我希望在表1中将此行复制为“stock”值时间。

I currently have the following query: 我目前有以下查询:

DECLARE @Count int = 1
WHILE @Count <= ....
BEGIN
    INSERT INTO table1 (table1.SKU, table1.timestamp_in) 
    SELECT table2.SKU, "some timestamp" FROM table2
    SET ...
END

I am not sure if this is the correct approach. 我不确定这是否是正确的方法。 I would like to run this loop for 'table2.stock' times. 我想为'table2.stock'时代运行这个循环。

My question is: Is this possible with just a SQL query, or should it be a better practice to build some (in my case) java code for this? 我的问题是:这可能仅仅是一个SQL查询,还是应该为这个构建一些(在我的情况下)java代码更好的做法?

You don't need a procedure or anything like that. 你不需要程序或类似的东西。 All you need is a table containing just numbers. 您只需要一个只包含数字的表格。 I'm creating this table on the fly with this in this example: 我在这个示例中动态创建此表:

SELECT aa.a + 10*bb.b + 100*cc.c AS numbers FROM (
SELECT 0 a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) aa 
, (SELECT 0 b UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) bb 
, (SELECT 0 c UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) cc;

This creates the numbers 0 till 999. 这将创建数字0到999。

Now you join your table2 with this numbers table in the range of stock . 现在,您将table2与stock范围内的此数字表联系起来。 Your final query looks like this: 您的最终查询如下所示:

INSERT INTO table1 (table1.SKU, table1.timestamp_in) 
SELECT table2.SKU, "some timestamp" FROM table2
INNER JOIN (
    SELECT aa.a + 10*bb.b + 100*cc.c AS n FROM (
    SELECT 0 a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) aa 
    , (SELECT 0 b UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) bb 
    , (SELECT 0 c UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) cc
) numbers ON numbers.n BETWEEN 0 AND table2.stock /*assuming you have no negative stock*/

Just make sure, that the numbers table contains more numbers than the highest value in the stock column. 只需确保数字表包含的数字多于stock列中的最高值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM