Revert backup table data to original table SQL
Only One Solution to Recover Data from Backup table is Rename Original table with random name and than rename Backup table with Original Table name in case if Identity Insert is ON for Original Table.
for example
Original Table - Invoice
Back Up Table - Invoice_back
Now Rename these tables :
Original Table - Invoice_xxx
Back Up Table - Invoice
In my case, INSERT INTO country SELECT * FROM county_bkp;
didnt work because:
- It wouldnt let me insert in Primary Key column due to
indentity_insert
being off by default. - My table had
TimeStamp
columns.
In that case:
- allow
identity_insert
in theOriginalTable
- insert query in which you mention all the columns of
OriginalTable
(ExcludingTimeStamp
Columns) and inValues
select all columns fromBackupTable
(ExcludingTimeStamp
Columns) - restrict identity_insert in the OriginalTable at the end.
EXAMPLE:
Set Identity_insert OriginalTable ON insert into OriginalTable (a,b,c,d,e, ....) --[Exclude TimeStamp Columns here] Select a,b,c,d,e, .... from BackupTable --[Exclude TimeStamp Columns here] Set Identity_insert OriginalTable Off