Quickest way to clone row in SQL

前端 未结 5 1854
鱼传尺愫
鱼传尺愫 2020-12-19 14:44

I want to clone multiple tables\' rows that have a single primary key (PrimKey). What\'s the quickest way to clone a row in SQL Server 2005?

Here\'s an example,

5条回答
  •  旧巷少年郎
    2020-12-19 15:17

    This is not the most beautiful solution, but I think it will work for you. First of all, you select your data into a temporary table with a "new" primary key and next you drop the old primary key column from the temp table and use the temp table to insert your "cloned" row.

    SELECT 
    'PrimKey2' AS NewPrimKey,
    *
    INTO #TMP 
    FROM PrimKeys 
    WHERE PrimKey='PrimKey1';
    
    ALTER TABLE #TMP DROP COLUMN PrimKey;
    
    INSERT INTO PrimKeys
    SELECT * FROM #TMP;
    

提交回复
热议问题