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

前端 未结 11 561
猫巷女王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: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

提交回复
热议问题