Solved

SQL Server: Select query from one database and insert into a table in another database

Posted on 2014-01-30
4
10,938 Views
Last Modified: 2014-02-17
Hi All,

I was wondering if it was at all possible in SQL Server to query one database and insert the results into a table in another database in one go?

Thanks,

OS
0
Comment
Question by:onesegun
  • 2
4 Comments
 
LVL 69

Expert Comment

by:Éric Moreau
Comment Utility
both on the same server?

insert into dbname.schema.tablename(c1,c2,c3)
select c1,c2,c3 from dbname2.schema.tablename2
0
 

Author Comment

by:onesegun
Comment Utility
No both on different servers.

Is it still possible?

Thanks,

OS
0
 
LVL 69

Assisted Solution

by:Éric Moreau
Éric Moreau earned 50 total points
Comment Utility
yes you can but you will have to use OpenRowset for your Select query. Check http://technet.microsoft.com/en-us/library/ms190312.aspx
0
 
LVL 16

Accepted Solution

by:
Surendra Nath earned 50 total points
Comment Utility
Solution 1

if you can establish a linked server connection on Server A to Server B
Then you can use the below code to do that

INSERT INTO <your Table>
SELECT * FROM [Server B].<Data base on server B>.<schema>.<Table on Server B>

Open in new window


Establishing a linked server is an easy process and a one time job...
You can follow the below tutorial to do the same

http://msdn.microsoft.com/en-us/library/aa560998.aspx
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
This video discusses moving either the default database or any database to a new volume.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now