Unix Technical Forum

Querying tables from different databases

This is a discussion on Querying tables from different databases within the SQL Server forums, part of the Microsoft SQL Server category; --> Hello, I am quite new to ms-sql and I have a problem : I want to create an SQL ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 06:12 PM
Henri
 
Posts: n/a
Default Querying tables from different databases

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.



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 06:12 PM
Dan Guzman
 
Posts: n/a
Default Re: Querying tables from different databases

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" <lemartin@nospam.inv> wrote in message
news:3f8065cb$0$27019$626a54ce@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.
>
>
>



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 06:12 PM
Anith Sen
 
Posts: n/a
Default Re: Querying tables from different databases

You can use 3-part naming like :

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

--
--- Anith


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-28-2008, 06:12 PM
Greg D. Moore \(Strider\)
 
Posts: n/a
Default Re: Querying tables from different databases


"Henri" <lemartin@nospam.inv> wrote in message
news:3f8065cb$0$27019$626a54ce@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.
>
>
>



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 10:16 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com