Merge two exact tables

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pcusters
    New Member
    • Jun 2007
    • 2

    Merge two exact tables

    I have two tables with the same structure and data.
    I want to merge this. I use the next query:

    insert into CUSTOMER_A
    select *
    from CUSTOMER_A_TEMP

    He gave an error with the primary key. The are the same.
    How can I make it happen that these will be differend?

    Pieter
  • Vidhura
    New Member
    • May 2007
    • 99

    #2
    Merge this into a seperate table or the one of the two tables?

    Comment

    • Motoma
      Recognized Expert Specialist
      • Jan 2007
      • 3236

      #3
      Originally posted by pcusters
      I have two tables with the same structure and data.
      I want to merge this. I use the next query:

      insert into CUSTOMER_A
      select *
      from CUSTOMER_A_TEMP

      He gave an error with the primary key. The are the same.
      How can I make it happen that these will be differend?

      Pieter
      You will not be able to insert your primary key.

      [code=sql]
      INSERT INTO CUSTOMER_A (Row2, Row3, Row4...all rows except the PK)
      (SELECT Row2, Row3, Row4...the same rows as above FROM CUSTOMER_A_TEMP )
      [/code]

      Comment

      • siva538
        New Member
        • Jun 2007
        • 44

        #4
        Originally posted by Motoma
        You will not be able to insert your primary key.

        [code=sql]
        INSERT INTO CUSTOMER_A (Row2, Row3, Row4...all rows except the PK)
        (SELECT Row2, Row3, Row4...the same rows as above FROM CUSTOMER_A_TEMP )
        [/code]
        This will work only if the pk column is identity in customer_A table.

        Comment

        • Motoma
          Recognized Expert Specialist
          • Jan 2007
          • 3236

          #5
          Originally posted by siva538
          This will work only if the pk column is identity in customer_A table.
          Good eye, you are correct with that statement.

          Comment

          • nomad
            Recognized Expert Contributor
            • Mar 2007
            • 664

            #6
            Originally posted by siva538
            This will work only if the pk column is identity in customer_A table.
            but they should be able to add a new field (ie PK) once the new table is set and go from there if the db is not to big.

            Comment

            • siva538
              New Member
              • Jun 2007
              • 44

              #7
              Originally posted by nomad
              but they should be able to add a new field (ie PK) once the new table is set and go from there if the db is not to big.
              I dont' think a new table is required in this situation. If the following steps are used sql is smart enough to take care of temp table creation during altering with identity
              1. Make sure that destination table has a primary key (automatically uniqueness is taken care)
              2. update destination table with the data from customer_a_temp where key matches (if any records exist with this kind)
              3. Alter this column (if not) to identity for ensuring future data from customer_temp
              4. finally use the above insert stmt for transferring data where keys does not exists

              let me know if you have still have any questions.

              Thanks,
              Sivakumar.

              Comment

              Working...