Best way to compare and update two SQL tables [closed] - sql

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
What is the best way to compare two tables lets say SourceTableA and DestinationTableB?
if data found in DestinationTableB but not existing in SourceTableA,
insert them to SourceTableA
else if data found in SourceTableA but not DestinationTableB, delete
them from SourceTableA

You should to use MERGE statement to do this.
MDSN - SQL Merge

Those two condition are mutually exclusive so you don't need an else
delete SourceTableA
where not exist (select 1 from DestinationTableB
where DestinationTableB.key = SourceTableA.key)
insert into SourceTableA
select *
from DestinationTableB
where not exist (select 1 from SourceTableA
where DestinationTableB.key = SourceTableA.key)

Relevant
This is very vague, but the best choice would probably to run 2 queries, one that delete from A where it doesn't exist in B, then one that inserts B into A where it doesn't exist in A.

Related

How can I do 'insert if not exists' in Ms sql server? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I want to move data from table1 to table2 (both the tables have same structure)with condition of comparing their emaild (unquie)in both the tables...if the data of emaild exits in table 2 I should not load that data otherwise I have to insert the record in table 2..these two tables are in two different databases of same sql server instance... Could you please provide me with the syntax.. Thanks in advance
INSERT db2.dbo.table2(key, other cols)
SELECT key, other cols
FROM db1.dbo.table1 AS t1
WHERE NOT EXISTS
(
SELECT 1
FROM db2.dbo.table2 AS t2
WHERE t2.key = t1.key
);
You can double-check you're getting the right rows by commenting out the first line.
Here's an example with a procedure on db<>fiddle.

How to update a table from one database to another? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have two database like A and B and a common table as TBL where the table TBL from A database has 1555 rows but the table TBL from database has only 1000 rows. I need to update the TBL in B database from A database.
1.How can I do it if these A and B database on same server name
2.what if these both database are on different server names .
Thanks in advance
INSERT INTO YourDbToBeInsertedInto..YourTable
SELECT *
FROM YourDbToBeInsertedFrom..YourTable
WHERE PrimaryKey NOT IN (SELECT PrimaryKey
FROM YourDbToBeInsertedInto..YourTable)
Use linked servers

Divide all values but one in sql table [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
i got this table, and i want to divide all the values on one of the columns except one.
I havent wrote any code about it just looking for an explanation on how to do it, if anyone could help out would appreciate.
In order to modify the content of one column in all rows except one, you can use the following query:
UPDATE tablename SET columName = columnName / 42 WHERE rowId !=42;
WHERE contains the condition that has to evaluate to true, in order for the update to take effect. My example modifies all rows except for those whose rowId column contains the value 42.

Overwriting Values From SQL Table to Another SQL Table [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have 2 databases, a live and backup. I want to overwrite the the values in a specific field in the live database from those that are in the backup. The database structure is identical the only difference is there name.
How do I do this?
Try this
UPDATE liveDB
SET column2 = src.column2 -----Whichever column you want
FROM BackupDB.dbo.Table1 AS src
INNER JOIN LiveDB.dbo.Table1 AS dest
ON src.column1 = dest.column1;
Updated
UPDATE live
SET l.ola_m_1= b.ola_m_1
FROM live.dbo.order_line l
JOIN backup.dbo.order_line b
ON --Whatever is Similar column between two

Sqlite: I need to update a data from one table to another [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have two tables in my sqlite database, with a column name as in both tables solution, solutionimage, id saying tableA and tableB. I want to, copy from tableB solution, solutionimage to tableA matching the id in both table respectively, how to do it?
I have google it and tried but i didnt get it.. Any one help me. Thanks a lot in advance.
Ideally you would want to join the table you are updating to the other table where you take the values from.
But I just read that JOINS in UPDATES are not allowed in SQLITE so subqueries are the way to go I suppose:
UPDATE tableB
SET
Solution = (SELECT Solution FROM tableA WHERE ID = tableB.ID),
SolutionImage = (SELECT Solution FROM tableA WHERE ID = tableB.ID);
See this fiddle for example output.