Insert from 2 tables into 1 table - sql

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

Related

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?

SQL query what to include in from statement

Say I have four tables.
Table 1:
PK_Column_a
Table 2
PK_Column_c
FK_Column_a
Table 3
FK_Column_c
FK_Column_e
PK_c,e
Table 4
PK_Column_e
If I now want write a SQL query that will select
table1.Column_a, table2.column_c, table4.Column_e
And I wish to connect them where their foreign keys are pointing (e.g. Where table1.Column_a = table2.Column_a).
Do I need to include table 3 in my "FROM" statement? or can I connect table 2 and table 4 without joining them through table 3?
I believe the answer is yes, you would need to join to Table-3, because otherwise you won't be able to bring in data from Table-4. (There's no other way to describe the relationship for the data in Table-4 to the data in Table-1 or Table-2.)
You have to join through the Table-3, otherwise you will generate a cross join and the data won't be valid. Simply every row of table 1&2 will merge with every row from Table 4 ...

Insert into multiple rows on Multiple tables

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;

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.

after running insert or update query, need the last inserted record and compare in vb.net sql server

i have 2 queries in vb.net with an if clause -
if x=0 then
insert into table1
else
update table1
both queries have 5 fields. now what i want to do is after this insert or update takes place, i need to look at this inserted/updated record and compare it with another table (table2). Especially for update, i have 5 fields in both tables. if any of the 5 fields dont match with table2, then i insert a new record in table 2 which is the updated record in table 1.
how can i do this?
You could use a trigger and keep this all on the database.