Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert three new rows for every result of a SELECT query into the same table

I am using SQL Server. For every:

select * from ServiceItems where Itemtypeid=7004 (query1)

I want to insert into the same table three new rows like:

(ItemID, PackageID, ItemTypeID, ServiceID, ItemName, CreatedDate) VALUES
(19377, 5352, 7007, 2011, N'L1', '11/11/2015 6:50:51 PM'), 
(19378, 5352, 7008, 2011, N'M1', '11/11/2015 6:50:51 PM'), 
(19376, 5352, 7006, 2011, N'W1', '11/11/2015 6:50:51 PM') 

ItemID = is the primary key
PackageID = one from query1
ItemTypeID = as it is 7006,7007,700
ServiceID = one from query1
ItemName =  as it is L1,M1,W1
CreatedDate = time now

I tried INSERT INTO SELECT...

INSERT INTO ServiceItems (PackageID, ItemTypeID, ServiceID, ItemName, CreatedDate)
SELECT PackageID, '7006', ServiceID, 'W1','' FROM ServiceItems WHERE ItemID = '7004'

but this one will add one row. Do I have to create three separate queries? How about using a cursor?

like image 356
Giannis Grivas Avatar asked Nov 14 '15 17:11

Giannis Grivas


People also ask

How to insert the result of a query in another table?

Inserting the result of a query in another table with order by An arranged order of rows (ascending or descending) of one table can also be inserted into another table by the use of SQL SELECT statement along with ORDER BY clause.

How to insert all the rows of another table in SQL?

All the rows or some rows of another table can also be inserted into the table using INSERT INTO statement. The rows of another table will be fetched based on one or more criteria using SQL SELECT statement.

What is the difference between insert into and insert multiple rows?

INSERT multiple rows in SQL is similar then INSERT INTO command, But its used to insert more then one rows in SQL table. Using INSERT INTO command in SQL , either you can insert new records direct from SQL query and Second, INSERT also used to insert/add records in a table from another existing table in SQL

How to query data from one table to another table?

You can use the INSERT statement to query data from one or more tables and insert it into another table as follows: INSERT INTO table1 (column1, column2) SELECT column1, column2 FROM table2 WHERE condition1;


1 Answers

You can use UNION ALL:

INSERT INTO ServiceItems (PackageID, ItemTypeID, ServiceID, ItemName, CreatedDate)

SELECT PackageID, '7006', ServiceID, 'W1', current_timestamp 
FROM ServiceItems 
WHERE ItemID = '7004'

UNION ALL

SELECT PackageID, '7007', ServiceID, 'L1', current_timestamp 
FROM ServiceItems 
WHERE ItemID = '7004'

UNION ALL

SELECT PackageID, '7008', ServiceID, 'M1', current_timestamp 
FROM ServiceItems 
WHERE ItemID = '7004'

Or better, a CROSS JOIN:

INSERT INTO ServiceItems (PackageID, ItemTypeID, ServiceID, ItemName, CreatedDate)
SELECT s.PackageID, x.ItemTypeId, s.ServiceID, x.ItemName, current_timestamp
FROM ServiceItems AS s 
CROSS JOIN (
  VALUES ('7006', 'W1'), 
         ('7007', 'L1'), 
         ('7008', 'M1')
) AS x (ItemTypeId, ItemName)
WHERE s.ItemID = '7004'
like image 145
Lukas Eder Avatar answered Oct 03 '22 21:10

Lukas Eder