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. Filling many to many rel table using Select statement

Filling many to many rel table using Select statement

Scheduled Pinned Locked Moved Database
helpcss
5 Posts 3 Posters 0 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.
  • I Offline
    I Offline
    indian143
    wrote on last edited by
    #1

    Hi All, I have Table A, TabB and TableC, I need to fill TableC by using Select statement and join on TableB and TableA two times. Because TableC is a many to many relationship table on TableA and TableB.

    TableA(Id, Name, Desc)
    TableB(Id, TableAId, AnotherTableAId, TableAName, AnotherTableAName)
    TableC(Id, TableAId, AnotherTableAId, TableAName, AnotherTableAName)

    Now the problem is TableA fills the new Data and TableC should be synced to TableB but should have new Ids from TableA and same TableAName, AnotherTableAName values from Old TableB rows. If I run as below, the rows aren't coming correctly, some times more rows than in TableB or some time less rows than in TableB. Here is how I tried but failed, any suggestion or link or even code snippet would help a lot.

    Insert into TableC (TableAId, AnotherTableAId, TableAName, AnotherTableAName)
    SELECT TableAId, AnotherTableAId, TableAName, AnotherTableAName FROM TableB b
    INNER JOIN TableA a ON a.Name = b. TableAName
    INNER JOIN TableA a2 ON a2.Name = b. AnotherTableAName

    Please help me with this, thanks in advance.

    Thanks, Abdul Aleem "There is already enough hatred in the world lets spread love, compassion and affection."

    M 1 Reply Last reply
    0
    • I indian143

      Hi All, I have Table A, TabB and TableC, I need to fill TableC by using Select statement and join on TableB and TableA two times. Because TableC is a many to many relationship table on TableA and TableB.

      TableA(Id, Name, Desc)
      TableB(Id, TableAId, AnotherTableAId, TableAName, AnotherTableAName)
      TableC(Id, TableAId, AnotherTableAId, TableAName, AnotherTableAName)

      Now the problem is TableA fills the new Data and TableC should be synced to TableB but should have new Ids from TableA and same TableAName, AnotherTableAName values from Old TableB rows. If I run as below, the rows aren't coming correctly, some times more rows than in TableB or some time less rows than in TableB. Here is how I tried but failed, any suggestion or link or even code snippet would help a lot.

      Insert into TableC (TableAId, AnotherTableAId, TableAName, AnotherTableAName)
      SELECT TableAId, AnotherTableAId, TableAName, AnotherTableAName FROM TableB b
      INNER JOIN TableA a ON a.Name = b. TableAName
      INNER JOIN TableA a2 ON a2.Name = b. AnotherTableAName

      Please help me with this, thanks in advance.

      Thanks, Abdul Aleem "There is already enough hatred in the world lets spread love, compassion and affection."

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      Look into UNION. Split your query into 2 by removing the second join.

      Insert into TableC (TableAId, AnotherTableAId, TableAName, AnotherTableAName)
      SELECT TableAId, AnotherTableAId, TableAName, AnotherTableAName FROM TableB b
      INNER JOIN TableA a2 ON a2.Name = b. AnotherTableAName
      UNION
      Insert into TableC (TableAId, AnotherTableAId, TableAName, AnotherTableAName)
      SELECT TableAId, AnotherTableAId, TableAName, AnotherTableAName FROM TableB b
      INNER JOIN TableA a ON a.Name = b. TableAName

      Never underestimate the power of human stupidity RAH

      K 1 Reply Last reply
      0
      • M Mycroft Holmes

        Look into UNION. Split your query into 2 by removing the second join.

        Insert into TableC (TableAId, AnotherTableAId, TableAName, AnotherTableAName)
        SELECT TableAId, AnotherTableAId, TableAName, AnotherTableAName FROM TableB b
        INNER JOIN TableA a2 ON a2.Name = b. AnotherTableAName
        UNION
        Insert into TableC (TableAId, AnotherTableAId, TableAName, AnotherTableAName)
        SELECT TableAId, AnotherTableAId, TableAName, AnotherTableAName FROM TableB b
        INNER JOIN TableA a ON a.Name = b. TableAName

        Never underestimate the power of human stupidity RAH

        K Offline
        K Offline
        KDGOLE
        wrote on last edited by
        #3

        --Insert into #TableC (TableAId, AnotherTableAId, TableAName, AnotherTableAName)
        SELECT TableAId, AnotherTableAId, TableAName, AnotherTableAName FROM #TableB b
        INNER JOIN #TableA a ON a.Name = b.TableAName OR a.Name = b.AnotherTableAName

        :) :) :)

        KDGole

        I 2 Replies Last reply
        0
        • K KDGOLE

          --Insert into #TableC (TableAId, AnotherTableAId, TableAName, AnotherTableAName)
          SELECT TableAId, AnotherTableAId, TableAName, AnotherTableAName FROM #TableB b
          INNER JOIN #TableA a ON a.Name = b.TableAName OR a.Name = b.AnotherTableAName

          :) :) :)

          KDGole

          I Offline
          I Offline
          indian143
          wrote on last edited by
          #4

          I did all of that, thanks for your help and what I felt was to use the Table variable and insert values into it, and then loop through table variable to check if that record combination doesn't exist in the table then insert the set. I like your approach too.

          Thanks, Abdul Aleem "There is already enough hatred in the world lets spread love, compassion and affection."

          1 Reply Last reply
          0
          • K KDGOLE

            --Insert into #TableC (TableAId, AnotherTableAId, TableAName, AnotherTableAName)
            SELECT TableAId, AnotherTableAId, TableAName, AnotherTableAName FROM #TableB b
            INNER JOIN #TableA a ON a.Name = b.TableAName OR a.Name = b.AnotherTableAName

            :) :) :)

            KDGole

            I Offline
            I Offline
            indian143
            wrote on last edited by
            #5

            I got it Kuldeep, thanks for your support, it is possible by using Merge and Table Variables.

            Thanks, Abdul Aleem "There is already enough hatred in the world lets spread love, compassion and affection."

            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