Results 1 to 9 of 9
  1. #1
    Join Date
    Apr 2006
    Posts
    13

    Unanswered: 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.

    Milfredo

  2. #2
    Join Date
    Jan 2006
    Location
    Los Angeles, CA
    Posts
    63
    You 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")

  3. #3
    Join Date
    Apr 2006
    Posts
    13
    Thank you very much.

    Milfredo

  4. #4
    Join Date
    May 2005
    Location
    South Africa
    Posts
    1,365
    Provided Answers: 1
    Quote Originally Posted by milfredo
    can you pull info from two databases in the same query
    Yes, e.g.
    Code:
    select l.sid, l.loginname, u.name
    from master..syslogins l
    ,msdb..sysusers u
    where l.sid=u.sid

  5. #5
    Join Date
    Apr 2006
    Posts
    13
    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

  6. #6
    Join Date
    Jan 2004
    Location
    In a large office with bad lighting
    Posts
    1,040
    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.

    -- This is all just a Figment of my Imagination --

  7. #7
    Join Date
    Apr 2006
    Posts
    6
    You can refer two different tables in two different databases provided they are on the same server otherwise a linked server may be required.

  8. #8
    Join Date
    Apr 2006
    Posts
    13
    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

  9. #9
    Join Date
    Apr 2006
    Posts
    13
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •