Solved

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

Posted on 2014-01-30
4
12,107 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 39820571
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
ID: 39820698
No both on different servers.

Is it still possible?

Thanks,

OS
0
 
LVL 70

Assisted Solution

by:Éric Moreau
Éric Moreau earned 50 total points
ID: 39820720
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
ID: 39821029
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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

740 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