By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,440 Members | 1,872 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,440 IT Pros & Developers. It's quick & easy.

Querying tables from different databases

P: n/a
Hello,

I am quite new to ms-sql and I have a problem : I want to create an SQL
request which would copy serveral records from a table in a given database
to another table (with exactly the same structure) in another database
(theses two tables and databases already exist).

Could you please tell me how to do this ? I dont know how to access two
different databases in a single SQL request.

Thank you for you help.

Jul 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
You can use 3-part names to qualify the object references:

INSERT INTO MyDatabase1.dbo.MyTable1
SELECT *
FROM MyDatabase2.dbo.MyTable2

--
Hope this helps.

Dan Guzman
SQL Server MVP

-----------------------
SQL FAQ links (courtesy Neil Pike):

http://www.ntfaq.com/Articles/Index....partmentID=800
http://www.sqlserverfaq.com
http://www.mssqlserver.com/faq
-----------------------

"Henri" <le******@nospam.inv> wrote in message
news:3f***********************@news.free.fr...
Hello,

I am quite new to ms-sql and I have a problem : I want to create an SQL request which would copy serveral records from a table in a given database to another table (with exactly the same structure) in another database
(theses two tables and databases already exist).

Could you please tell me how to do this ? I dont know how to access two different databases in a single SQL request.

Thank you for you help.

Jul 20 '05 #2

P: n/a
You can use 3-part naming like :

USE database1
GO
INSERT tbl(col1, col2, ...coln)
SELECT col1, col2, ..coln
FROM database2.dbo.tbl ;

--
--- Anith
Jul 20 '05 #3

P: n/a

"Henri" <le******@nospam.inv> wrote in message
news:3f***********************@news.free.fr...
Hello,

I am quite new to ms-sql and I have a problem : I want to create an SQL
request which would copy serveral records from a table in a given database
to another table (with exactly the same structure) in another database
(theses two tables and databases already exist).

Could you please tell me how to do this ? I dont know how to access two
different databases in a single SQL request.

use northwind
go
select * from pubs.dbo.authors
go
Basically you need to use the db name and the object owner.
Thank you for you help.

Jul 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.