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

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

提交回复
热议问题