Insert into multiple rows on Multiple tables - sql

Inserting rows into table one by one is more hardy and tedious instead of inserting rows into more than one tables at one time with just in single SQL query
Because I have 10 tables in which I need to insert rows so it would be more boring to put rows only in one table at a time rather than all rows inserts at all tables
So please suggest me a better query to insert all rows at all tables in one time

For multitable insert you can use the below syntax if you have to insert a limited set of records at 1 go:
INSERT ALL
INTO AA(A,B,C,D,E,F) VALUES (1,2,3,4,5,6)
INTO AB(A,B,C,D,E) VALUES (5,4,3,4,2)
SELECT * FROM DUAL;

Related

COPY FROM / INSERT Millions of Rows Into Same PostgreSQL table

I have a table with hundreds of millions of rows that I need to essentially create a "duplicate" of each existing row in, doubling its row count. I'm currently using an insert operation (and unlogging the table prior to inserting) which still takes a long while as one transaction. Looking for guidance on if there may be a more efficient way to execute the query below.
INSERT INTO costs(
parent_record, is_deleted
)
SELECT id, is_deleted
FROM costs;

Merge into multiple table using two different SELECT clause from a WITH statement (ORACLE)

I have a procedure refreshed daily, I want to insert / or merge into 2 table from a single source, but I don't want to create one then delete and insert data to a table just only for the source (T1 table) (I don't use that source table for reporting and it have many rows so the procedure might run slowly)
My desired result is:
Merge into or insert into 2 existing tables using
with t1 table
and two select query from that t1 table (the two select query have different purpose, one is map the detail (supposed group by ID) and one is group by month,... etc from t1 table)
From what I have researched so far, insert all only support single query and insert to multiple table, and merge into only merge into 1 table with 1 single query.
How to combine these?

Insert from 2 tables into 1 table

I would like to insert columns from 2 different tables into one table.
The following is not working on the second Insert. Is this possible to do this way? the receiving table is currently empty. the idea is to have one row in the third table per the 2 inserts.
INSERT INTO CA1665AFTT.EMODESADV3
(E3ecsn, e3mena, e3hand)
SELECT e1ecsn, e1mena, e1hand
FROM CA1665AFTT.EMODESADV1
INSERT INTO CA1665AFTT.EMODESADV3
(E3CPRD, E3CQTY)
SELECT prdc, oqty
FROM Monica.emod
Yes it's possible using a join between the two tables

insert from one table into two tables

I have three tables:
Table A has columns name, id, nationality
Table B has a column name
Table C has a column id
I was wondering if it is possible to extract from Table A and insert its name column into Table B and id column into Table C in one single SQL query? Not in two separate queries.
I know it is possible in Oracle.
I am using Teradata, which supports all SQL queries.
It is not possible to do in a single query. One table at a time only. Use a Transaction or a Stored statement to query the data and then two more queries to insert the data in each table. This does save you making the query for both inserts but you cant do an INSERT on two tables.

Query select a bulk of IDs from a table - SQL

I have a table which holds ~1M rows. My application has a list of ~100K IDs which belong to that table (the list being generated by the application layer).
Is there a common-method of how to query all of these IDs? ~100K Select queries? A temporary table which I insert the ~100K IDs to, and Select query via join the required table?
Thanks,
Doori Bar
You could do it in one query, something like
SELECT * FROM large_table WHERE id IN (...)
Insert a comma-separated list of IDs where I put the ...
Unfortunately, there is no easy way that I know of to parametrize this, so you need to be extra-super careful to avoid SQL injection vulnerabilities.
A temporary table which holds the 100k IDs seems like a good solution. Don't insert them one by one though ; INSERT ... VALUES syntax in MySQL accepts the insertion of multiple rows.
By the way, where do you get your 100k IDs, if it's not from the database ? If they come from a preceding request, I'd suggest to have it fill the temporary table.
Edit : For a more portable way of multiple insert :
INSERT INTO mytable (col1, col2) SELECT 'foo', 0 UNION SELECT 'bar', 1
Do those id's actually reference the table with 1M rows?
If so, you could use SELECT * ids FROM <1M table>
where ids is the ID column and where "1M table" is the name of the table which holds the 1M rows.
but I don't think I really understand your question...