Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Select from one database table where column value equals a column value in a different database

Select from one database table where column value equals a column value in a different database

Scheduled Pinned Locked Moved Database
databasesysadminhelp
6 Posts 3 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    moordoom
    wrote on last edited by
    #1

    I am trying to pull all data from SQL Database A, Table 1 where column name is X, and the value in column X equals the value in Database B, Table 2, column name Y. So I have 2 databases on the same server. One named A, the other named B. I need to get all the data from A, Table1, where the values of Column Name X are equal to the values in in Column Name Y in B, Table2, and insert/write them to a new table in Database B. Any and all help is appreciated. My SQL writing skills are very sub par. If the title, and the subject differ, I apologize. I am just trying to put the thought thru my head as I write.

    G J 2 Replies Last reply
    0
    • M moordoom

      I am trying to pull all data from SQL Database A, Table 1 where column name is X, and the value in column X equals the value in Database B, Table 2, column name Y. So I have 2 databases on the same server. One named A, the other named B. I need to get all the data from A, Table1, where the values of Column Name X are equal to the values in in Column Name Y in B, Table2, and insert/write them to a new table in Database B. Any and all help is appreciated. My SQL writing skills are very sub par. If the title, and the subject differ, I apologize. I am just trying to put the thought thru my head as I write.

      G Offline
      G Offline
      GuyThiebaut
      wrote on last edited by
      #2

      This is the general gist of where I think you need to be heading:

      insert into B.NewTable
      select a1.*
      from A.Table1 a1
      join B.Table2 b2
      on a1.X = b2.Y

      “That which can be asserted without evidence, can be dismissed without evidence.”

      ― Christopher Hitchens

      M 1 Reply Last reply
      0
      • G GuyThiebaut

        This is the general gist of where I think you need to be heading:

        insert into B.NewTable
        select a1.*
        from A.Table1 a1
        join B.Table2 b2
        on a1.X = b2.Y

        “That which can be asserted without evidence, can be dismissed without evidence.”

        ― Christopher Hitchens

        M Offline
        M Offline
        moordoom
        wrote on last edited by
        #3

        a1 and b2 are columns? what is X and Y?

        G 1 Reply Last reply
        0
        • M moordoom

          a1 and b2 are columns? what is X and Y?

          G Offline
          G Offline
          GuyThiebaut
          wrote on last edited by
          #4

          a1 and b2 are aliases for tables.

          moordoom wrote:

          what is X and Y?

          They are the columns you mentioned in your question. I think you need to read a book on sql or get some basic training, since if you cannot make sense of my answer you need to start reading.

          “That which can be asserted without evidence, can be dismissed without evidence.”

          ― Christopher Hitchens

          1 Reply Last reply
          0
          • M moordoom

            I am trying to pull all data from SQL Database A, Table 1 where column name is X, and the value in column X equals the value in Database B, Table 2, column name Y. So I have 2 databases on the same server. One named A, the other named B. I need to get all the data from A, Table1, where the values of Column Name X are equal to the values in in Column Name Y in B, Table2, and insert/write them to a new table in Database B. Any and all help is appreciated. My SQL writing skills are very sub par. If the title, and the subject differ, I apologize. I am just trying to put the thought thru my head as I write.

            J Offline
            J Offline
            joginder banger
            wrote on last edited by
            #5

            you can solve your problem with the help of SQL join(Inner Join).Sql Join on W3school

            M 1 Reply Last reply
            0
            • J joginder banger

              you can solve your problem with the help of SQL join(Inner Join).Sql Join on W3school

              M Offline
              M Offline
              moordoom
              wrote on last edited by
              #6

              Looking at the W3 school, it shows table to table joins, not database to database joins. Here is what I have so far... (minus the insert, and it works) SELECT * from Table1 join Database2.Table2 on ColumnX = ColumnY X is a Column in Database1.Table1, and Y is a Column in Database2.Table2. This gets me my info when I run the query on Database1. But now I want write these info into Database2.Table3 and delete what was in there before. So would my statement then be...?? DELETE * FROM Database2.Table3 INSERT INTO Database2.Table3 SELECT * from Table1 join Database2.Table2 on ColumnX = ColumnY

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups