Duplicate row with T-SQL in SQL Server

SQL server

Assume we have a table: MyTable with an identity column Id. We would like to duplicate this row and change one of the fields - MyField


SELECT *
INTO #temp 
FROM MyTable
WHERE MyField='old_value';

ALTER TABLE #temp DROP COLUMN ID;

UPDATE #temp SET MyField = 'new_value' WHERE MyField='old_value'

INSERT INTO MyTable SELECT * FROM #temp;

DROP TABLE #temp



Post a Comment

Previous Post Next Post