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.
Related
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?
I have 2 tables in my SQL database:
And I want to merge them in a way the result will be:
This is just an example for 2 tables which need to be merged into one new table (The tables contain an example data, the statement should work for any amount of data inside the tables).
The ID which got different value in CSV should be updated into the new table for example:
ID 3's value is 'KKK' and in table T is 'CCC', then what should be updated is the CSV table.
You seem to want a left join and to match to the second table if available:
select t.id, coalesce(csv.value, t.value) as value
from t left join
csv
on t.id = csv.id;
If you want this in a new table, use the appropriate construct for your database, or use insert to insert into an existing table.
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 ...
This is an extension of Insert values into table B based on column from table A?
From the above question, let's say in both the User_Permissions and Users table there's also 2 more columns recorded for audit purposes: a version column and a transaction_version column. When inserting the new row (which is based on a row from the Users table) into the User_Permissions table I need to take the value of the 2 columns in the Users table, increment it by 1 and then insert it into the User_Permissions table.
Is there an easy SQL query to do this? I suspect it'd have to do with another inline select but am unsure of the syntax.
You could use Triggers after insert to perform the needed updates
I am creating a trigger and understand how to do this if the columns in two different tables have the same name. Ex, I write a trigger that if a new record is added in the salary column for table A to take the salary from table A and insert that as salary in table B.
What I don't understand how to do, is populate that data into table B if that table has a different column name. So I have table A that the column name is salary and table B where the column name is pastsalary.
How do I write the trigger to be able to do that?
I don't see the difference between the two.. Your insert statment would just reflect the column you want to insert into:
insert into tableB(pastsalary) values (:new.salary);