How to Insert Table Values from One Database to Another Database

Insert data from one database to another database if record does not exist

I think you can try to use exists and not exists to make it.

USE Destination
INSERT INTO Destination.dbo.Table(Column1, Column2, Column3 etc...)
SELECT t1.Column1, t1.Column2, t1.Column3 etc...
FROM Source.dbo.Table t1
WHERE
exists (
SELECT 1
FROM Destination.dbo.Table t2
WHERE t2.Column1 = t1.Column1
)
And
not exists (
SELECT 1
FROM Destination.dbo.Table t2
WHERE t2.Column2 = t1.Column2
)

Insert values of one table in a database to another table in another database

INSERT INTO studies
(
id
,name_string
,description
,field
,is_active
,register_start
,register_end
)

SELECT nextval('studiesSequence')
,NAME
,''
,3
,0
,register_start
,register_end
FROM dblink('dbname=mydb', 'select name,begins,ends from study')
AS t1(NAME VARCHAR(50), register_start DATE, register_end DATE);

You can directly insert values that retured by dblink()(that means no need to create a view)

MySQL Insert Into from one Database in another

Your query should go like this:

INSERT INTO newDatabase.table1 (Column1, Column2) 
SELECT column1, column2 FROM oldDatabase.table1;

UPDATE

Since this answer is getting more attention than I even anticipated, I should expand on this answer. First of all, it might not be obvious from the answer itself, but the columns do not need to have the same name. So, following will work too (assuming that the columns exist in their respective tables):

INSERT INTO newDatabase.table1 (Column1, Column2) 
SELECT SomeOtherColumn, MoreColumns FROM oldDatabase.table1;

Furthermore, they don't even need to be real columns in the table. One of the examples for transforming data that I use quite often is:

INSERT INTO newDatabase.users (name, city, email, username, added_by) 
SELECT CONCAT(first_name, ' ', last_name), 'Asgard', CONCAT(first_name,'@gmail.com'), CONCAT(first_name,last_name), 'Damir' FROM oldDatabase.old_users;

So, as it might be more obvious now, the rule is, as long as the SELECT query returns same number of columns that INSERT query needs, it can be used in place of VALUES.

INSERT INTO database from another database, specifying some values in SQL Server

Unless I'm missing something, you could use:

INSERT INTO DB2.T2(ID, Description, Enable, Code ,Date) 
SELECT ID, Description, 1 ,null,getdate() FROM DB1.T1;

When you start to think about looping, stop and revisit the problem. Almost always a better way.

If ID should be incrementing and not based on what is in DB1.T1, look into making that column in DB2 IDENTITY(). If you do, then you would just leave the ID off of the insert, and SQL Server will handle that itself.

Insert data from db to another db

If there is a lot of data to transfer and multiple tables, I would suggest using Import/Export wizard provided by SQL Server Management Studio.

http://www.mssqltips.com/sqlservertutorial/203/simple-way-to-import-data-into-sql-server/

Edit:
However, if there is not lot of data and the two systems are not connected - and you need to generate script to transfer data, your query should look like this:

SELECT 'INSERT INTO Countries (Id, Name) VALUES (' + CAST(countryid AS VARCHAR(50)) + ', ''' + countryname + ''')' from Country

How to insert table of one database to another database table automatically

You can use Jobs and schedule them as per your requirement



Related Topics



Leave a reply



Submit