How can I do 'insert if not exists' in Ms sql server? [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 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.

Related

How to get data from a table with a certain prefix? [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 2 years ago.
Improve this question
I have a table where the name is like "id,name" and I know the id portion and I need to get all the rows from the table by giving that id. The ID is always unique for the table. I've tried selecting the table out from information_schema.tables and I can get the table selected but then can't figure out how to get the data from it.
You're looking to use AS (alias):
SELECT test1.id AS id_test1, t2.id AS id_test2
FROM table1 AS test1
INNER JOIN table2 AS t2 ON (...)

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

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.

Update field with data from another database [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
Need to update a field with data from another field in a different database
I Have two SQL commercial Databases from the same company, the first Database has one field that is null in the other
I need to update the Field/Database that is null with the data of the first one.
MS SQL Server
Update table1 in current database from table1 in database called "DataBaseName"
update table1
set col2 = T2.col2
from DataBaseName.dbo.table1 as T2
where table1.ID = T2.ID and
table1.col2 is null