How to copy a row and insert in same table with a autoincrement field in MySQL?

后端 未结 13 1055
旧时难觅i
旧时难觅i 2020-11-29 15:55

In MySQL I am trying to copy a row with an autoincrement column ID=1 and insert the data into same table as a new row with

13条回答
  •  庸人自扰
    2020-11-29 16:36

    A lot of great answers here. Below is a sample of the stored procedure that I wrote to accomplish this task for a Web App that I am developing:

    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON
    
    -- Create Temporary Table
    SELECT * INTO #tempTable FROM  WHERE Id = Id
    
    --To trigger the auto increment
    UPDATE #tempTable SET Id = NULL 
    
    --Update new data row in #tempTable here!
    
    --Insert duplicate row with modified data back into your table
    INSERT INTO  SELECT * FROM #tempTable
    
    -- Drop Temporary Table
    DROP TABLE #tempTable
    

提交回复
热议问题