Multiple SQL Databases

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

    #1

    Multiple SQL Databases

    I'm working on an app where I need to access a table from 2 different
    databases (same MS-SQL box).

    What I need is to have a SELECT string with an INNER JOIN based on these 2
    tables. If they were in the same DB this would be no problem, but I can't
    figure out how to get this via C#.

    Any help would be greatly appreciated, thx!

    -Gary


  • Ignacio Machin \( .NET/ C#  MVP \)

    #2
    Re: Multiple SQL Databases

    Hi Gary,

    This is a 100% SQL question :)


    This is how you can access a table from another DB in the same DB-server, if
    the BDs are in different servers then you need to create a link server.

    select * from OTHER_DB_NAME.o wner.Table_Name

    where owner is the owner of the table, by default dbo

    Cheers,

    --
    Ignacio Machin,
    ignacio.machin AT dot.state.fl.us
    Florida Department Of Transportation

    "Gary" <gary.rey@NOOOO SPAM.virtium.co m> wrote in message
    news:%23Y8KiWam DHA.2404@TK2MSF TNGP12.phx.gbl. ..[color=blue]
    > I'm working on an app where I need to access a table from 2 different
    > databases (same MS-SQL box).
    >
    > What I need is to have a SELECT string with an INNER JOIN based on these 2
    > tables. If they were in the same DB this would be no problem, but I can't
    > figure out how to get this via C#.
    >
    > Any help would be greatly appreciated, thx!
    >
    > -Gary
    >
    >[/color]


    Comment

    • Gary

      #3
      Re: Multiple SQL Databases

      But doesn't the connection string have the "initial catalog" set to one DB?
      Then how would that select call be able to access the second DB?

      -Gary


      "Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us > wrote
      in message news:%23xcCOcam DHA.684@TK2MSFT NGP09.phx.gbl.. .[color=blue]
      > Hi Gary,
      >
      > This is a 100% SQL question :)
      >
      >
      > This is how you can access a table from another DB in the same DB-server,[/color]
      if[color=blue]
      > the BDs are in different servers then you need to create a link server.
      >
      > select * from OTHER_DB_NAME.o wner.Table_Name
      >
      > where owner is the owner of the table, by default dbo
      >
      > Cheers,
      >
      > --
      > Ignacio Machin,
      > ignacio.machin AT dot.state.fl.us
      > Florida Department Of Transportation
      >
      > "Gary" <gary.rey@NOOOO SPAM.virtium.co m> wrote in message
      > news:%23Y8KiWam DHA.2404@TK2MSF TNGP12.phx.gbl. ..[color=green]
      > > I'm working on an app where I need to access a table from 2 different
      > > databases (same MS-SQL box).
      > >
      > > What I need is to have a SELECT string with an INNER JOIN based on these[/color][/color]
      2[color=blue][color=green]
      > > tables. If they were in the same DB this would be no problem, but I[/color][/color]
      can't[color=blue][color=green]
      > > figure out how to get this via C#.
      > >
      > > Any help would be greatly appreciated, thx!
      > >
      > > -Gary
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • Wim Hollebrandse

        #4
        Re: Multiple SQL Databases

        As long as your 'initial catalog' database server contains the linked server entry - which can be configured with pre-defined login credentials, then you're fine.

        Cheers,
        Wim Hollebrandse

        Zealand IT Ltd is a UK based IT consultancy and software development company that specialises in bespoke Microsoft .NET solutions including ASP.NET web applications and advanced SOA architectures for public and private sector clients. Please contact us for a free quote.


        ---
        Posted using Wimdows.net Newsgroups - http://www.wimdows.net/newsgroups/

        Comment

        • JKJ

          #5
          Re: Multiple SQL Databases

          Linked server isn't necessary in this particular case -
          he states that the databases are hosted by same SQL
          Server instance on the same machine (at least that is how
          I understood the question). Fully qualifying the table
          names should suffice.

          (ansi92 sql)
          SELECT [some stuff]
          FROM [database].[owner].[table] a,
          [database].[owner].[table] b
          WHERE
          a.[field] = b.[field]

          Even if the databases are on two distinct machines,
          linked servers aren't always necessary. Simply use the
          OPENROWSET or OPENDATASOURCE function where you'd use a
          linked server. But I would agree that if you are joining
          these tables all the time, or executing distributed
          transactions, linking the servers is probably the best
          approach.

          Overall, I agree with Ignacio, though, this has nothing
          to do with C# ;-) Hope we helped anyway.

          JKJ, MCSD.NET
          [color=blue]
          >-----Original Message-----
          >As long as your 'initial catalog' database server[/color]
          contains the linked server entry - which can be
          configured with pre-defined login credentials, then
          you're fine.[color=blue]
          >
          >Cheers,
          >Wim Hollebrandse
          >http://www.wimdows.net
          >http://www.wimdows.com
          >
          >---
          >Posted using Wimdows.net Newsgroups -[/color]
          http://www.wimdows.net/newsgroups/[color=blue]
          >.
          >[/color]

          Comment

          • Ignacio Machin \( .NET/ C#  MVP \)

            #6
            Re: Multiple SQL Databases

            Hi Gary,

            Yes, of course you have an initial catalog, but it does not imply you
            cannot access others DBs in the same server ( or in others servers using
            linking ).

            Anyway try it and see if it works

            Cheers,

            --
            Ignacio Machin,
            ignacio.machin AT dot.state.fl.us
            Florida Department Of Transportation

            "Gary" <gary.rey@NOOOO SPAM.virtium.co m> wrote in message
            news:ehLtZhamDH A.424@TK2MSFTNG P10.phx.gbl...[color=blue]
            > But doesn't the connection string have the "initial catalog" set to one[/color]
            DB?[color=blue]
            > Then how would that select call be able to access the second DB?
            >
            > -Gary
            >
            >
            > "Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us >[/color]
            wrote[color=blue]
            > in message news:%23xcCOcam DHA.684@TK2MSFT NGP09.phx.gbl.. .[color=green]
            > > Hi Gary,
            > >
            > > This is a 100% SQL question :)
            > >
            > >
            > > This is how you can access a table from another DB in the same[/color][/color]
            DB-server,[color=blue]
            > if[color=green]
            > > the BDs are in different servers then you need to create a link server.
            > >
            > > select * from OTHER_DB_NAME.o wner.Table_Name
            > >
            > > where owner is the owner of the table, by default dbo
            > >
            > > Cheers,
            > >
            > > --
            > > Ignacio Machin,
            > > ignacio.machin AT dot.state.fl.us
            > > Florida Department Of Transportation
            > >
            > > "Gary" <gary.rey@NOOOO SPAM.virtium.co m> wrote in message
            > > news:%23Y8KiWam DHA.2404@TK2MSF TNGP12.phx.gbl. ..[color=darkred]
            > > > I'm working on an app where I need to access a table from 2 different
            > > > databases (same MS-SQL box).
            > > >
            > > > What I need is to have a SELECT string with an INNER JOIN based on[/color][/color][/color]
            these[color=blue]
            > 2[color=green][color=darkred]
            > > > tables. If they were in the same DB this would be no problem, but I[/color][/color]
            > can't[color=green][color=darkred]
            > > > figure out how to get this via C#.
            > > >
            > > > Any help would be greatly appreciated, thx!
            > > >
            > > > -Gary
            > > >
            > > >[/color]
            > >
            > >[/color]
            >
            >[/color]


            Comment

            Working...