Efficient Insert Query Against Multiple Tables in MySQL - sql

I was wondering if there is a more efficient way of doing an insert in MySQL against multiple tables than a separate insert query for each record in each table. I was thinking of doing something like this:
INSERT INTO table1
(t1c1, t1c2, t1c3), table2 (t2c1, t2c2, t2c3)
VALUES
('t1c1', 't1c2', 't1c3', 't2c1', 't2c2', 't2c3');
The reason for this is that the data is collated on a remote machine and will be doing the insert over the network.

No, there is no way of doing this in a single step. You will need to perform multiple queries.

You could insert into one table first, then into the second one from the first table:
INSERT INTO table1 ....
VALUES(....
INSERT INTO table2(....
SELECT ...
FROM table1
WHERE ....

Related

Select from inherited table

I have this inheritance in my database and I need to use a SELECT query and INSERT query to it.
I can't seem to pull this off.
It's about the Item and it's inheritances.
This could be helpful?
INSERT INTO table2
SELECT *
FROM table1
WHERE condition;
I don't know if i've understood your question but, you want to insert some values into a table selecting from an another table.

How can I INSERT data into two tables simultaneously with only one sql script db2?

How would I insert into multiple tables with one sql script in db2
For example, insert a row into T1 DOCK_DOOR and then insert into T2 DOCK_DOOR_LANE multiple times based on the dock_door_sysid from the first table.
My first approach was the following. I was attempting to use a with with three inserts. on the other hand, doing to inserts on the second table is not and option if this can be automated with one insert.
thanks for any feedback
sql example
WITH ins AS (
INSERT INTO DBF1.DOCK_DOOR (DOCK_DOOR_SYSID,DOOR_NUMBER,DOOR_NAME,DOCK_SYSID,DOOR_SEQ,ENCRYPTION_CODE,RFID_ENBLD_FLAG,LANES_COUNT,CMNT_TEXT,CREATE_TS,CREATE_USERID,UPDATE_TS,UPDATE_USERID,VER_NUMBER,ACTIVE_FLAG,STATUS_SYSID,DOOR_TYPE_SYSID)
VALUES (nextval for DBF1.DOCK_DOOR_SEQ,'026','DOOR025',61,25,NULL,'N','2',NULL,current timestamp,'SQL_INSERT',current timestamp,'SQL_INSERT',0,NULL,1723,1142)
RETURNING door_number,dock_door_sysid),
ins2 AS (
INSERT INTO SIT.DOCK_DOOR_lane (DOCK_DOOR_LANE_SYSID,DOOR_LANE_ID,DOCK_DOOR_SYSID,LANE_ID,CREATE_TS,CREATE_USERID,UPDATE_TS,UPDATE_USERID,VER_NUMBER)
VALUES (nextval for DBF1.DOCK_DOOR_LANE_seq,door_number||''||'A',dock_door_sysid,'A',current timestamp},'SQL_INSERT',current timestamp,'SQL_INSERT',0)
SELECT door_number,dock_door_sysid FROM DBF1.DOCK_DOOR
RETURNING door_number,dock_door_sysid)
INSERT INTO DBF1.DOCK_DOOR_lane (DOCK_DOOR_LANE_SYSID,DOOR_LANE_ID,DOCK_DOOR_SYSID,LANE_ID,CREATE_TS,CREATE_USERID,UPDATE_TS,UPDATE_USERID,VER_NUMBER)
VALUES (nextval for DBF1.DOCK_DOOR_LANE_seq,door_number||''||'B',dock_door_sysid,'B',current timestamp},'SQL_INSERT',current timestamp,'SQL_INSERT',0)
SELECT door_number,dock_door_sysid FROM DBF1.DOCK_DOOR;
Table 1 = dock_door
Table 2 = Dock_door_lane
You could do it with a trigger on the dock_door table.
However, if you're on a recent, version on IBM i. You might be able to make use of data change table reference
Your statement would look something like this
insert into dock_door_lane
select <....>
from final table (insert into dock_door <...>)
I'm not sure it will work, as this article indicates that at least at a couple of years ago DB2 for i didn't support the secondary insert required.
This old SO question also seems to confirm that at least at v7.1, the double insert isn't supported.
If I get a chance, I'll run a test on a 7.2 system Monday.

Save return values from INSERT...RETURNING into temp table (PostgreSQL)

I have a table table1 with columns id,value1 and value2.
Also I have a query
INSERT INTO table1(value1,value2) SELECT value3,value4 FROM table2 RETURNING id
that returns set of ids.
I want to store return values (these ids) in some temp table. Something like that:
INSERT INTO TEMP temp1 INSERT INTO table1(value1,value2) SELECT value3,value4 FROM table2 RETURNING id
How can I do it?
DBMS is PostgreSQL
with inserted as (
INSERT INTO table1 (value1,value2)
SELECT value3,value4
FROM table2
RETURNING id
)
insert into temp
select id
from inserted;
This requires Postgres 9.2 or later.
Two options.
If you need it just for one follow-up query, a with statement (see the horse's answer) is the easiest.
If you need it for more than one follow-up query, the other option is to not use insert ... returning, but rather create table as:
CREATE TEMPORARY TABLE foo AS
SELECT value3,value4 FROM table2
Caveats: if necessary, create the indexes you need on the table -- and analyze it if you do.

how can I get a variable with all the results and iterate over it?

I have a table with some records, about 40, called Table1.
I would like to create a new register in another table for each these records, but I don't know how can I do it. I would like to iterate with a while loop for each record and insert the record in the other table, but I don't how (the syntax).
I would like something like this:
foreach record in myTable1
Insert into Table2(IDTable1) VALUES(record.IDTable1)
end foreach
I don't know scripts in SQL Server.
Thanks.
If you have only insertion, you can do this:
INSERT INTO Table2 (IDTable1)
SELECT IDTable1 FROM myTable1
It will select all IDTable1 from myTable1 and insert them into Table2.
SQL Server, for the most part, is set-based. That means that queries and rows are down in sets, group and filtered by clauses. Only in rare cases will you want or need to iterate over every single row in a result set and do something with it.
I'd say in your case, you're wanting to copy the contents of one table to another. You can do that several ways, but one of the easiest is something like this:
SELECT
IDTable1
INTO
Table2
FROM
Table1
This assumes that Table2 does not exist, and will create it. If Table2 already exists, your syntax will change to be like what other answers indicated:
INSERT INTO Tabel2( IDTable1 )
SELECT
IDTable1
FROM
Table1

SQL Server - Inserting Record To Many Tables At A Time

I have a stored proc that executes insert onto 4 tables on SQL Server database. These 4 tables have exactly the same structure. How can I perform the insert at a time?
Maybe something like:
INSERT INTO Table1, Table2, Table3, Table4
VALUES (#p1, #p2, ....... #pn)
There isnt a way - this is four separate inserts.
You could put all results into a single temp table and then select into the other tables to reduce the code, but you cannot do all four at once.
See:
Transact-sql insert in two tables at once?
You can't insert into multiple tables in a single INSERT statement. To ensure the insert into all four tables is atomic, you wrap the inserts in a transaction. Pseudocode:
BEGIN TRANSACTION
INSERT INTO Table1(...) VALUES(...)
INSERT INTO Table2(...) VALUES(...)
INSERT INTO Table3(...) VALUES(...)
INSERT INTO Table4(...) VALUES(...)
COMMIT
You just need to separate them, per say they cant be combined into one statement but this would work equally as well.
INSERT INTO Table1
VALUES (#p1, #p2)
INSERT INTO Table2
VALUES (#p1, #p2)
INSERT INTO Table3
VALUES (#p1, #p2)
INSERT INTO Table4
VALUES (#p1, #p2)
You could set up one table to be the "master" and INSERT only to it. You'd then have to create a trigger on that table to push the changes into any others which need the new rows. Other posters are correct - it's four INSERTs, but this could simplify client code. While making it confusing as well... =/