MSSQL Insert values from table2 into table1 using condition from table2 - sql

I have two tables: table1 and table2 I need to migrate some data from table2 into table1 by matching or using a condition from both tables.
What I need is something like:
INSERT INTO Tbl1(VAR1,VAR2)
SELECT VAR1, VAR2
FROM Tbl2
WHERE Tbl1.Name = Tbl2.Name and Tbl1.City = Tbl2.City
I have a sample schema here

It seems you need update not insert
update Tbl1
set Tbl1.VAR1= Tbl2.VAR1,
Tbl1.VAR2= Tbl2.VAR2
FROM Tbl2 join Tbl1
on Tbl1.Name = Tbl2.Name and Tbl1.ADDRESS = Tbl2.ADDRESS
one line demo

You could use a inner join for select the right result from both the tables
INSERT INTO Tbl1(VAR1,VAR2)
SELECT VAR1, VAR2
FROM Tbl2
INNER JOIN tbl1 ON Tbl1.Name = Tbl2.Name
and Tbl1.City = Tbl2.City

Related

SQL how to insert from another table based on same row values?

I have two tables with similar column values for columns "name" and "regis". One table is already filled with values, the other has only values for "name". How do i get "regis" values from table1 into table2 based on same column values?
I tried something like this
INSERT INTO table2 (regis)
SELECT table1 (regis)
WHERE table2.name = table1.name
Please try:
update table2 set regis = (select regis from table1 where table1.name=table2.name);
see the whole sample on:
https://www.db-fiddle.com/f/3jC8PGeZZEuty3XVq8gdzz/9
I think, what you are after is insert when name not found in table2 or update if its found in both tables.
INSERT INTO TABLE2 (regis)
SELECT t1.REGIS FROM TABLE1 as t1
LEFT OUTER JOIN TABLE2 as t2 on t2.name = t1.name
WHERE t2.name is null
You can update table2 using the statement like below:
update t2
set t2.regis = t1.regis
from table2 as t2
inner join table1 as t1 on t1.name = t2.name
If you are using SQL Server, then you can use merge statement to either insert or update in one step.
You need to use table2 as join in your select statement.
INSERT INTO table2 (regis)
SELECT table1.regis FROM Table1 INNER JOIN Table2 on table1.name = table2.name

Inner join the tables where the value of one column is different. But the meaning of the values are same

Table t1
Table t2
I tried running the query
select CountyCode, ContactPerson
from table1 t1
inner join select * from table 2 t2 on t1.CountyCode[1] = Code[0]
Any one help me please.
Use this syntax:
select CountyCode, ContactPerson
from t1
inner join t2 on t1.CountyCode = t2.Code

INSERT INTO ... SELECT with more SELECT columns

Thank you for your time.
I have a INSERT INTO ... SELECT setup, but I also want to filter using extra columns (that do not exist in the table I am inserting into)
For example:
INSERT INTO table1 (t1_col1, t1_col2, t1_col3)
SELECT
t2.t1_col1,
t2.t1_col2,
t3.t1_col3,
t4.filter_col
FROM table2 t2
INNER JOIN table3 t3 ON t2.t1_col1 = t3.t1_col1
INNER JOIN table4 t4 ON t4.filter_col = t2.filer_col
WHERE t4.filter_col = 'value';
table1 only has columns t1_col1, t1_col2 and t1_col3 so when I attempt to run this it fails as expected:
ERROR: INSERT has more expressions than target columns
So my question is, how can I still include the filter, but specify which columns from my SELECT statement should be used in the INSERT INTO statement.
Many thanks for any help!
you have to same number of column in select ,so remove t4.filter_col from selection
INSERT INTO table1 (t1_col1, t1_col2, t1_col3)
SELECT
t2.t1_col1,
t2.t1_col2,
t3.t1_col3
FROM table2 t2
INNER JOIN table3 t3 ON t2.t1_col1 = t3.t1_col1
INNER JOIN table4 t4 ON t4.filter_col = t2.filer_col
WHERE t4.filter_col = 'value';
Note: you are not bound to select all columns those you used in join or filter
It's almost right, but you select one column too much. Try this:
INSERT INTO table1 (t1_col1, t1_col2, t1_col3)
SELECT
t2.t1_col1,
t2.t1_col2,
t3.t1_col3
FROM table2 t2
INNER JOIN table3 t3 ON t2.t1_col1 = t3.t1_col1
INNER JOIN table4 t4 ON t4.filter_col = t2.filer_col
WHERE t4.filter_col = 'value';

create a sql join with more columns

i want create a sql statemnt (in PL SQL Developer) with a join with comma seperated?
SELECT * FROM TABLE1 t1 JOIN TABLE2 t2 ON t1.tab_id, second_id = t2.tab_id, second_id;
I always get a ORA-00920 Exception. If i change it to two Rows:
t1.tab_id = t2.tab_id AND t1.second_id = t2.second_id;
Then i get rows.
Can some say me if i can use the first step with coma seperated columns?
Greetz
You need a valid condition:
SELECT *
FROM TABLE1 t1 JOIN
TABLE2 t2
ON t1.tab_id = t2.tab_id AND t1.second_id = t2.second_id;
I think Oracle will also let you do:
SELECT *
FROM TABLE1 t1 JOIN
TABLE2 t2
ON (t1.tab_id, t1.second_id) in ( (t2.tab_id, t2.second_id) );
Or even:
SELECT *
FROM TABLE1 t1 JOIN
TABLE2 t2
USING (tab_id, second_id);
This works because the JOIN keys have the same names in the two tables.

MS Sql - Update table 1 based on table 2 and table 3

I want to update table 1 based on table 2 and table 3. Right now am updating it in two separate statements.
Is it possible to update table 1 first based from table 2 then for the NULLS to be updated from table 3 all in a single update using join.
I have tried to build a query but where to include
tbl1.col1=tbl3.col1
update tbl1
set tbl1.col1= tbl2.col1
from table1 tbl1
left JOIN table2 tbl2
ON tbl1.col = tbl2.col
left JOIN table3 tbl3
on tbl1.col=tbl3.col and tbl2.col<>tbl3.col
I think you just want coalesce():
update tbl1
set col1 = coalesce(tbl2.col1, tbl3.col1)
from table1 tbl1 left join
table2 tbl2
on tbl1.col = tbl2.col left join
table3 tbl3
on tbl1.col = tbl3.col and tbl2.col <> tbl3.col;
Use COALESCE or ISNULL both returns the first NOT NULL value
Try this
UPDATE tbl1
SET tbl1.col1 = COALESCE(tbl2.col1, tbl3.col1) -- ISNULL(tbl2.col1, tbl3.col1)
FROM table1 tbl1
LEFT JOIN table2 tbl2
ON tbl1.col = tbl2.col
LEFT JOIN table3 tbl3
ON tbl1.col = tbl3.col
AND tbl2.col <> tbl3.col