How do you copy a record in a SQL table but swap out the unique id of the new row?

前端 未结 11 524
猫巷女王i
猫巷女王i 2020-11-30 19:09

This question comes close to what I need, but my scenario is slightly different. The source table and destination table are the same and the primary key is a uniqueidentifie

相关标签:
11条回答
  • 2020-11-30 19:17

    Specify all fields but your ID field.

    INSERT INTO MyTable (FIELD2, FIELD3, ..., FIELD529, PreviousId)
    SELECT FIELD2, NULL, ..., FIELD529, FIELD1
    FROM MyTable
    WHERE FIELD1 = @Id;
    
    0 讨论(0)
  • 2020-11-30 19:19

    Here is how I did it using ASP classic and couldn't quite get it to work with the answers above and I wanted to be able to copy a product in our system to a new product_id and needed it to be able to work even when we add in more columns to the table.

    Cn.Execute("CREATE TEMPORARY TABLE temprow AS SELECT * FROM product WHERE product_id = '12345'")
    Cn.Execute("UPDATE temprow SET product_id = '34567'")
    Cn.Execute("INSERT INTO product SELECT * FROM temprow")
    Cn.Execute("DELETE temprow")
    Cn.Execute("DROP TABLE temprow")
    
    0 讨论(0)
  • 2020-11-30 19:23

    Try this:

    
    insert into MyTable(field1, field2, id_backup)
        select field1, field2, uniqueId from MyTable where uniqueId = @Id;
    

    Any fields not specified should receive their default value (which is usually NULL when not defined).

    0 讨论(0)
  • 2020-11-30 19:24

    I have the same issue where I want a single script to work with a table that has columns added periodically by other developers. Not only that, but I am supporting many different versions of our database as customers may not all be up-to-date with the current version.

    I took the solution by Jonas and modified it slightly. This allows me to make a copy of the row and then change the primary key before adding it back into the original source table. This is also really handy for working with tables that do not allow NULL values in columns and you don't want to have to specify each column name in the INSERT.

    This code copies the row for 'ABC' to 'XYZ'

    SELECT * INTO #TempRow FROM SourceTable WHERE KeyColumn = 'ABC';
    UPDATE #TempRow SET KeyColumn = 'XYZ';
    INSERT INTO SourceTable SELECT * FROM #TempRow;
    DELETE #TempRow;
    

    Once you have finished the drop the temp table.

    DROP TABLE #TempRow;
    
    0 讨论(0)
  • 2020-11-30 19:27

    If "key" is your PK field and it's autonumeric.

    insert into MyTable (field1, field2, field3, parentkey)
    select field1, field2, null, key from MyTable where uniqueId = @Id
    

    it will generate a new record, copying field1 and field2 from the original record

    0 讨论(0)
  • 2020-11-30 19:29

    You can do like this:

    INSERT INTO DENI/FRIEN01P 
    SELECT 
       RCRDID+112,
       PROFESION,
       NAME,
       SURNAME,
       AGE, 
       RCRDTYP, 
       RCRDLCU, 
       RCRDLCT, 
       RCRDLCD 
    FROM 
       FRIEN01P      
    

    There instead of 112 you should put a number of the maximum id in table DENI/FRIEN01P.

    0 讨论(0)
提交回复
热议问题