Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 390
  • Last Modified:

Inserting SQL

Hi,

I have two separate databases.  DB01 and DB02

DB01 contains data that is not present in DB02 and the task here is to get data from DB01 into DB02.

I have so far written an SQL statement to get the data that I need displayed.  Can this be easily turned into the relevant insert statements?

Any other suggestions appreciated.  I’m using SQL Server  Management Studio?

Thanks in advance.
0
andyw27
Asked:
andyw27
1 Solution
 
sameer_goyalCommented:
Hi,

I am sure i can help. But I will need a sample query that you have to displaying the data.

I can then quickly give you an equivalent insert query.

Also, help me understand if both the databases are on the same server?
0
 
Peter KipropCommented:
Hi  andyw27,

I hope the below query can give u a hint on how to go about the insertion of data.
insert DB02.TableName
select * from DB01.TableName a
where not exists (select 1 from DB02.TableName b where b.ColumnName = a.ColumnName )

Open in new window

In the above example column ColumnName ought to be a unique field that identifies the record in the table.

Hope it helps.

Pthepebble.
0
 
Olaf DoschkeSoftware DeveloperCommented:
As you already have a query displaying the data you want in db02, just make sure it has all fields in the table field order and you can do

insert into db02.dbo.table
(....your select from db01.dbo.table here...)

The only other prerequisite is both databases are on the same server instance, so you can address the tables as db01.dbo.table and db02.dbo.table.

Bye, Olaf.
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now