Query to copy records to another database

I have Database A with a bunch of records, on table 'users', database name lets call it "DBONE", i have datbase "DBTWO", this one has no tables at all.

Can i create a query to select certain records and copy them to a new table on DBTWO ?

Something like:

Select * from users where clientid = 2

Then add to it to copy it to DBTWO with the same table structure and the data that matches the query ?
LVL 1
AleksAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
this is the starting point:
insert into dbtwo.dbo.users 
Select * from dbone.dbo.users where clientid = 2

Open in new window


note that if the destination table has an identity column, you need to either skip that column (see snippet below) or use identity insert

insert into dbtwo.dbo.users (col1, col2, col3 ... )
Select col1, col2, col3 ... 
from dbone.dbo.users where clientid = 2

Open in new window

or:

use dbtwo
set identity_insert dbo.users on
into dbo.users (idcol, col1, col2, col3 ... )
Select idcol, col1, col2, col3 ... 
from dbone.dbo.users where clientid = 2
set identity_insert dbo.users off

Open in new window


hope this helps
0
AleksAuthor Commented:
Will the above work even though the table is NOT created in DB two ?  will it be created automatically ?
0
AleksAuthor Commented:
In fact I noticed i need to recreate the table on DB two. Is there an easy way to do this ?
Take the same columns from tableA in DBONE and create the same table with NO data on DBTWO ?

I would do this, then copy the data.

A
0
Protecting & Securing Your Critical Data

Considering 93 percent of companies file for bankruptcy within 12 months of a disaster that blocked access to their data for 10 days or more, planning for the worst is just smart business. Learn how Acronis Backup integrates security at every stage

Guy Hengel [angelIII / a3]Billing EngineerCommented:
no problem:
use dbtwo
Select idcol, col1, col2, col3 ... 
into dbo.users  
from dbone.dbo.users where clientid = 2

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AleksAuthor Commented:
Perfect !!! You are awesome as always ! .. this will be such a great time saver. thanks !!!
0
AleksAuthor Commented:
Great response.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.