Thursday, March 22, 2012

Duplicate a Row of Data MS Sql

I want to be able to duplicate a row of data in sql...Does anyone know if there is a sql command that will do that. I have a table with an auto increment primary key and I want to duplicate everything except the key into a new record.

Thanks.Given a table with an ID IDENTITY column, and First and Last Name columns, to duplicate row with 1234 as ID:

INSERT INTO MyTable(FirstName,LastName)
SELECT FirstName, LastName FROM MyTable WHERE ID=1234|||What if there can be many tables that I'm duplicating from but I don't know what all the field names will be. Basically I want to be able to pass a table name and a primary key and I want to duplicate that record.

Any ideas?

No comments:

Post a Comment