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

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

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
onesegun
Asked:
onesegun
  • 2
2 Solutions
 
Éric MoreauSenior .Net ConsultantCommented:
both on the same server?

insert into dbname.schema.tablename(c1,c2,c3)
select c1,c2,c3 from dbname2.schema.tablename2
0
 
onesegunAuthor Commented:
No both on different servers.

Is it still possible?

Thanks,

OS
0
 
Éric MoreauSenior .Net ConsultantCommented:
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
 
Surendra NathTechnology LeadCommented:
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
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

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

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