Skip to main content

How to duplicate the last record through SQL query

Dear Readers,
Today i share a code which may be help you out once in a blue, last few days back i found i request from my customer about duplicating the record in to new case for saving the time.

I have done this through query and i want to share this with you, I hope it will save your time.


INSERT INTO Table_Name
( field1, field2, field3,...)
SELECT top(1) field1, filed2, filed3, ...  FROM Table_Name  WHERE primaryKey = yourvariable

Make sure do not include the primary key + autonumber field in this query.

Also you need to make sure the sequence of the inserting field (field1, field2, field3...) will be in same sql field sequence


If you want to add any variable in the field here is the sample query
Declare @var1 int
Declare @var2 int
Set @var1 = 1
Set @var2 = 2
insert into Table_Name(field1, field2, field3,...)
select * Column_Name, @var1, @var2  from second_table where condition

I hope it save your time

Regards
Rashid Imran Bilgrami
http://www.bestvisualization.com



Comments