如何使用基于另一个select语句的循环插入到表中?

时间:2021-03-21 22:44:23

I have a select query which returns some IDs.

我有一个select查询,它返回一些id。

SELECT GROUP_ID FROM GROUP_REQUEST_MAPPING WHERE REQUEST_TYPE_ID = 1

As a result I get this.

结果我得到了这个。

GROUP_ID
6
7
8
9
14

I have to loop through the IDs and then insert that many rows in another table.

我必须遍历这些id,然后在另一个表中插入许多行。

INSERT INTO REQ_TASK VALUES(_,_,_,IDs)

How do I do that. I am new to sql. Thanks

我该怎么做。我是sql新手。谢谢

3 个解决方案

#1


3  

Directly use the constants or parameters with the select like below:

直接使用常量或参数,选择如下:

INSERT INTO REQ_TASK VALUES(_,_,_,IDs)
SELECT @param1,@param2,'xyz', GROUP_ID FROM GROUP_REQUEST_MAPPING WHERE REQUEST_TYPE_ID = 1

Here is a small example

这里有一个小例子

 Create table #food
( item varchar(50))

insert into #food values 
('icecream'),
('sandwich'),
('Pasta'),
('FrenchFries'),
('Toast')

--Create another table #food_test

——# food_test创建另一个表

Create table #food_test
( item varchar(50),quantity int)

Insert into #food_test(item,quantity)
select item,10 from #food

Now check the value in #food_test

现在检查#food_test中的值。

select * from #food_test

#2


0  

You can use:

您可以使用:

INSERT INTO REQ_TASK (col1, ...) 
SELECT GROUP_ID FROM GROUP_REQUEST_MAPPING 
WHERE REQUEST_TYPE_ID = 1;

#3


0  

You can directly use your query to insert in another table via insert into statement as

可以通过insert into语句as直接使用查询插入另一个表

USE MASTER

 Create table GROUP_REQUEST_MAPPING
( GROUP_ID int, REQUEST_TYPE_ID int)

insert into GROUP_REQUEST_MAPPING values 
(6,1),
(7,1),
(8,1),
(9,1),
(10,1)
Create table REQ_TASK (AMOUNT int, IDs int)
SELECT * FROM GROUP_REQUEST_MAPPING
SELECT * FROM REQ_TASK

INSERT INTO REQ_TASK (AMOUNT,IDs)
SELECT 10, GROUP_ID FROM GROUP_REQUEST_MAPPING WHERE REQUEST_TYPE_ID = 1

SELECT * FROM REQ_TASK

DROP TABLE REQ_TASK
DROP TABLE GROUP_REQUEST_MAPPING

https://www.w3schools.com/sql/sql_insert_into_select.asp

https://www.w3schools.com/sql/sql_insert_into_select.asp

#1


3  

Directly use the constants or parameters with the select like below:

直接使用常量或参数,选择如下:

INSERT INTO REQ_TASK VALUES(_,_,_,IDs)
SELECT @param1,@param2,'xyz', GROUP_ID FROM GROUP_REQUEST_MAPPING WHERE REQUEST_TYPE_ID = 1

Here is a small example

这里有一个小例子

 Create table #food
( item varchar(50))

insert into #food values 
('icecream'),
('sandwich'),
('Pasta'),
('FrenchFries'),
('Toast')

--Create another table #food_test

——# food_test创建另一个表

Create table #food_test
( item varchar(50),quantity int)

Insert into #food_test(item,quantity)
select item,10 from #food

Now check the value in #food_test

现在检查#food_test中的值。

select * from #food_test

#2


0  

You can use:

您可以使用:

INSERT INTO REQ_TASK (col1, ...) 
SELECT GROUP_ID FROM GROUP_REQUEST_MAPPING 
WHERE REQUEST_TYPE_ID = 1;

#3


0  

You can directly use your query to insert in another table via insert into statement as

可以通过insert into语句as直接使用查询插入另一个表

USE MASTER

 Create table GROUP_REQUEST_MAPPING
( GROUP_ID int, REQUEST_TYPE_ID int)

insert into GROUP_REQUEST_MAPPING values 
(6,1),
(7,1),
(8,1),
(9,1),
(10,1)
Create table REQ_TASK (AMOUNT int, IDs int)
SELECT * FROM GROUP_REQUEST_MAPPING
SELECT * FROM REQ_TASK

INSERT INTO REQ_TASK (AMOUNT,IDs)
SELECT 10, GROUP_ID FROM GROUP_REQUEST_MAPPING WHERE REQUEST_TYPE_ID = 1

SELECT * FROM REQ_TASK

DROP TABLE REQ_TASK
DROP TABLE GROUP_REQUEST_MAPPING

https://www.w3schools.com/sql/sql_insert_into_select.asp

https://www.w3schools.com/sql/sql_insert_into_select.asp