How to query two MS SQL DB's on the same server inside a Stored Procedure

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • JMack

    #1

    How to query two MS SQL DB's on the same server inside a Stored Procedure

    Okay, so I have a problem and I would be REALLY grateful for any
    assistance anyone can offer because I have found little or no help on
    the web anywhere.

    I want to access and do joins between tables in two different SQL db's
    on the same server. Heres what Im dealing with.

    In one database resides all of my security features for our clients,
    where it decides who can login, etc etc....

    In another database, I need to cross reference with a few fields in my
    security db.

    See the issue Im running into here is that because the way the people
    have their databases set up for different products, I would normally
    have to put these tables with security features in every database...
    which is horrible, because every time I do an update I would have to
    do it in 12 different places. Thats not efficient at all.

    So I thought if I had one central DB, where all security features are
    controlled from, that would be perfect... now the issue is cross
    referencing and doing joins with other tables that ARENT in the same
    db....


    have I lost you yet?

    I appreciate all of your help!

    THANKS!!
  • Simon Hayes

    #2
    Re: How to query two MS SQL DB's on the same server inside a Stored Procedure

    google@digitall sd.com (JMack) wrote in message news:<472b479f. 0402170642.37a5 7121@posting.go ogle.com>...[color=blue]
    > Okay, so I have a problem and I would be REALLY grateful for any
    > assistance anyone can offer because I have found little or no help on
    > the web anywhere.
    >
    > I want to access and do joins between tables in two different SQL db's
    > on the same server. Heres what Im dealing with.
    >
    > In one database resides all of my security features for our clients,
    > where it decides who can login, etc etc....
    >
    > In another database, I need to cross reference with a few fields in my
    > security db.
    >
    > See the issue Im running into here is that because the way the people
    > have their databases set up for different products, I would normally
    > have to put these tables with security features in every database...
    > which is horrible, because every time I do an update I would have to
    > do it in 12 different places. Thats not efficient at all.
    >
    > So I thought if I had one central DB, where all security features are
    > controlled from, that would be perfect... now the issue is cross
    > referencing and doing joins with other tables that ARENT in the same
    > db....
    >
    >
    > have I lost you yet?
    >
    > I appreciate all of your help!
    >
    > THANKS!![/color]

    As a general answer to your question, you can write code like this:

    select *
    from dbo.ThisTable t1
    join ThatDatabase.db o.ThatTable t2
    on t1.KeyColumn = t2.KeyColumn

    Assuming you have SQL2000 (you didn't mention the version), you should
    review the "Using Ownership Chains" topic in Books Online for
    information about cross-database ownership chains (and there is an
    article in the current SQL Server Magazine also).

    Simon

    Comment

    • Jeremy Mack

      #3
      Re: How to query two MS SQL DB's on the same server inside a Stored Procedure

      well unfortunately I'm in SQL 7 so that option doesnt apply to me.

      The only other way I can see around it, is taking the tables I need
      available to all the db's and replicating them from one publishing db...

      which is overkill, but because of the way the system is set up this is
      the only other option i can think of aside from replication is trying to
      get IT to updgrade to SQL 2000

      *** Sent via Developersdex http://www.developersdex.com ***
      Don't just participate in USENET...get rewarded for it!

      Comment

      • Jeremy Mack

        #4
        Re: How to query two MS SQL DB's on the same server inside a Stored Procedure



        Oh damn buddy,

        you have saved my life. IT WORKS.


        THANK YOU SO MUCH!

        *** Sent via Developersdex http://www.developersdex.com ***
        Don't just participate in USENET...get rewarded for it!

        Comment

        • Simon Hayes

          #5
          Re: How to query two MS SQL DB's on the same server inside a Stored Procedure


          "Jeremy Mack" <google@digital lsd.com> wrote in message
          news:4032799b$0 $199$75868355@n ews.frii.net...[color=blue]
          > well unfortunately I'm in SQL 7 so that option doesnt apply to me.
          >
          > The only other way I can see around it, is taking the tables I need
          > available to all the db's and replicating them from one publishing db...
          >
          > which is overkill, but because of the way the system is set up this is
          > the only other option i can think of aside from replication is trying to
          > get IT to updgrade to SQL 2000
          >
          > *** Sent via Developersdex http://www.developersdex.com ***
          > Don't just participate in USENET...get rewarded for it![/color]

          Sorry if my explanation was misleading - joining on another database will
          work fine in SQL7, but in SQL2000 the default cross-database chaining
          behaviour changed in SP3, which is why it's worth reviewing the
          documentation.

          Simon


          Comment

          • Erland Sommarskog

            #6
            Re: How to query two MS SQL DB's on the same server inside a Stored Procedure

            JMack (google@digital lsd.com) writes:[color=blue]
            > Okay, so I have a problem and I would be REALLY grateful for any
            > assistance anyone can offer because I have found little or no help on
            > the web anywhere.
            >
            > I want to access and do joins between tables in two different SQL db's
            > on the same server. Heres what Im dealing with.
            >
            > In one database resides all of my security features for our clients,
            > where it decides who can login, etc etc....
            >
            > In another database, I need to cross reference with a few fields in my
            > security db.
            >
            > See the issue Im running into here is that because the way the people
            > have their databases set up for different products, I would normally
            > have to put these tables with security features in every database...
            > which is horrible, because every time I do an update I would have to
            > do it in 12 different places. Thats not efficient at all.
            >
            > So I thought if I had one central DB, where all security features are
            > controlled from, that would be perfect... now the issue is cross
            > referencing and doing joins with other tables that ARENT in the same
            > db....[/color]

            I see that you have got a solution working.

            But I am a little wary of hard-coding database references. The day
            you need to set up a test environment on the same server, you have
            trouble...

            One alternative would be to have a central database which you maintain,
            and then use replication to push those updates to the other places.
            Although admittedly, replication might be a little heavy-duty for this...


            --
            Erland Sommarskog, SQL Server MVP, sommar@algonet. se

            Books Online for SQL Server SP3 at
            SQL Server 2025 redefines what's possible for enterprise data. With developer-first features and integration with analytics and AI models, SQL Server 2025 accelerates AI innovation using the data you already have.

            Comment

            Working...