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. Need a way to clone rows in SQL [modified]

Need a way to clone rows in SQL [modified]

Scheduled Pinned Locked Moved Database
databasecsharpvisual-studiotutorialquestion
7 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.
  • P Offline
    P Offline
    PyroManiak
    wrote on last edited by
    #1

    I'm literally trying to clone data rows, nothing very complicated, infact, they aren't even going to be changed during this section of the project. I've captured the rows I want to clone using a custom SQL query, but I'm not sure how to insert them back into the database with new ID's(primary key is autonumber)... can't I just run a .update of some sort? I've tried copying the data table from results of my query and merging into the complete table, that doesn't make any change. I dont seem to be able to .insert anything other then independant values for each column. I'm using VS 2005 and C#. Thanks. -- modified at 17:09 Monday 24th July, 2006

    M 1 Reply Last reply
    0
    • P PyroManiak

      I'm literally trying to clone data rows, nothing very complicated, infact, they aren't even going to be changed during this section of the project. I've captured the rows I want to clone using a custom SQL query, but I'm not sure how to insert them back into the database with new ID's(primary key is autonumber)... can't I just run a .update of some sort? I've tried copying the data table from results of my query and merging into the complete table, that doesn't make any change. I dont seem to be able to .insert anything other then independant values for each column. I'm using VS 2005 and C#. Thanks. -- modified at 17:09 Monday 24th July, 2006

      M Offline
      M Offline
      Michael Potter
      wrote on last edited by
      #2

      Just run an insert ignoring the autonumber primary key. It will be filled in for you.

      INSERT INTO MyTable
      (
         Field1,
         Field2,
         Field3
      )
      SELECT
         Field1,
         Field2,
         Field3
      FROM
         MyTable
      WHERE
         Field2 = 'MyCriteria'
      
      P 1 Reply Last reply
      0
      • M Michael Potter

        Just run an insert ignoring the autonumber primary key. It will be filled in for you.

        INSERT INTO MyTable
        (
           Field1,
           Field2,
           Field3
        )
        SELECT
           Field1,
           Field2,
           Field3
        FROM
           MyTable
        WHERE
           Field2 = 'MyCriteria'
        
        P Offline
        P Offline
        PyroManiak
        wrote on last edited by
        #3

        I'm trying that, and I dont think I'm doing something right in the query. Is field1 supposed to be the primary key?(as per default)? I'm getting "primary key doesn't allow nulls" error, and my field1 is set to the autonum. By autonum, I mean that SQL is set for INT for primary key, and I've got my dataset flagged to incriment 1, seed 1. I think this is the correct way to set for autonum? I'm used to there being a default "autonum" from access for the field type... -- modified at 18:26 Monday 24th July, 2006

        F M 2 Replies Last reply
        0
        • P PyroManiak

          I'm trying that, and I dont think I'm doing something right in the query. Is field1 supposed to be the primary key?(as per default)? I'm getting "primary key doesn't allow nulls" error, and my field1 is set to the autonum. By autonum, I mean that SQL is set for INT for primary key, and I've got my dataset flagged to incriment 1, seed 1. I think this is the correct way to set for autonum? I'm used to there being a default "autonum" from access for the field type... -- modified at 18:26 Monday 24th July, 2006

          F Offline
          F Offline
          Frank Kerrigan
          wrote on last edited by
          #4

          try..... insert into Employees SELECT * from Employees where name = 'Joe' Watch out for Unquie fields and indentity.


          Look where you want to go not where you don't want to crash. Bikers Bible

          1 Reply Last reply
          0
          • P PyroManiak

            I'm trying that, and I dont think I'm doing something right in the query. Is field1 supposed to be the primary key?(as per default)? I'm getting "primary key doesn't allow nulls" error, and my field1 is set to the autonum. By autonum, I mean that SQL is set for INT for primary key, and I've got my dataset flagged to incriment 1, seed 1. I think this is the correct way to set for autonum? I'm used to there being a default "autonum" from access for the field type... -- modified at 18:26 Monday 24th July, 2006

            M Offline
            M Offline
            Michael Potter
            wrote on last edited by
            #5

            Do not specify the primary key. A new one will be generated for you. Note: If you expect to get a full clone (copy the primary key also) then you will never accomplish this in a single table. Primary keys can not be duplicated.

            P 1 Reply Last reply
            0
            • M Michael Potter

              Do not specify the primary key. A new one will be generated for you. Note: If you expect to get a full clone (copy the primary key also) then you will never accomplish this in a single table. Primary keys can not be duplicated.

              P Offline
              P Offline
              PyroManiak
              wrote on last edited by
              #6

              If I use INSERT INTO tbABC SELECT (all but primary key) FROM tbABC WHERE Name = @Name I get an error back saying that the Column Name or number of supplied values does not match the table definition. If I use INSERT INTO tbABC SELECT * FROM tbABC WHERE Name = @Name I get a name saying that the primary key can't be duplicated (duh)... I've been looking all over the table design and properties in SQL and I can't find where I can actually pick an autonumber for the field type. I thought that inside my dataset I'd set to AutoIncriment, seed 1, step 1.. isn't this correctly setting autonumbering for an SQL database? I'm used to access just having autonum as a field type choice. Thanks.

              P 1 Reply Last reply
              0
              • P PyroManiak

                If I use INSERT INTO tbABC SELECT (all but primary key) FROM tbABC WHERE Name = @Name I get an error back saying that the Column Name or number of supplied values does not match the table definition. If I use INSERT INTO tbABC SELECT * FROM tbABC WHERE Name = @Name I get a name saying that the primary key can't be duplicated (duh)... I've been looking all over the table design and properties in SQL and I can't find where I can actually pick an autonumber for the field type. I thought that inside my dataset I'd set to AutoIncriment, seed 1, step 1.. isn't this correctly setting autonumbering for an SQL database? I'm used to access just having autonum as a field type choice. Thanks.

                P Offline
                P Offline
                PyroManiak
                wrote on last edited by
                #7

                Sweet! I just found out I can flag the primary key as the identity column, now it works!!! Yay! Thanks alot for your help guys. -- modified at 15:04 Tuesday 25th July, 2006

                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