• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 297
  • Last Modified:

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 ?
0
Aleks
Asked:
Aleks
  • 4
  • 2
1 Solution
 
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
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
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
 
AleksAuthor Commented:
Perfect !!! You are awesome as always ! .. this will be such a great time saver. thanks !!!
0
 
AleksAuthor Commented:
Great response.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

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