Friday, February 24, 2012

accessing two databases at once

I'm new to databases as you probably can tell. A friend sent me three data bases, which in my mind should have been one database with all the tables included, but he didn't. So my question is, can you pull info from two databases in the same query?

I write the query acessing one database and the result chart must depend on a where predicate based on accessing tables in another database. If this isn't possible, how do I get the tables I need together, that presently reside in differing databases?

Thanks for the help in advance.

MilfredoYou could access two databases at the same time if you need to.
Example: to copy Table1 from a database called OtherDB to a database called MyDB you can do the following:

select * into MyDB..Table1 from OtherDB..Table1

(Note: the previous will not create constraints nor will it create indexes in MyDB..Table1. You could create the tables first ant then use "insert into")|||Thank you very much.

Milfredo|||can you pull info from two databases in the same queryYes, e.g.select l.sid, l.loginname, u.name
from master..syslogins l
,msdb..sysusers u
where l.sid=u.sid|||Thanks for trying to help. But I'm just a simple guy at home trying to learn to work with SQL. You lost me with the Master reference and basically the whole syntax of the FROM clause.

Thanks anyway.
Milfredo|||Try this ... open up Books Online. Click on the Index tab. Type the following (without the quotes) ... "SELECT, FROM clause". Note the database.owner.table naming structure.|||You can refer two different tables in two different databases provided they are on the same server otherwise a linked server may be required.|||Thanks very much. I just got home from My Daughter's opening day in her new dress shop. So I will tend to database business tomorrow. Thanks again.

Milfred|||I either didn't see it or didn't understand what I was reading. Probably both. So I'm going to try this. I'm going to provide some info, very basic, and see if someone will put it in code for me, so, if I see it, maybe I'll understand.

Two databases: EQRaces EQCharts

While I know this code will be wrong, it should describe visually what I'm trying to do.

Select Horsename AS Name

FROM EQRaces, EQCharts

WHERE PowerRating > 50

Okay...Horsename is in EQCharts and PowerRating is in EQraces

Can someone please show me the right code?

Thanks,
Milfredo

No comments:

Post a Comment