Question : insert missing deleted rows in a table from a backup

I mistakenly deleted about 100 rows from a table with 20,000 rows in it. No one else has been working in the database since the time of my backup. Can anyone come up with a SQL statement to retrieve the deleted rows from the backup database and insert them back into the production table?

I've restored the backup to a database on the same server but with its name having "Backup" appened to it. One complication is that the primary key for the table is a combination of two columns.

I can do this with VB code that loops through the pre-deletion table, checks the production table, if the row's not there then inserts it. But it seems there might be a SQL statement that could accomplish this rather than looping through the code.

Answer : insert missing deleted rows in a table from a backup

OK, so you can try like this:
assume f1 and f2 is the 2 key fields in your table
1:
2:
3:
4:
5:
6:
7:
8:
9:
select t.* from
(
select backupdatabasename.dbo.table1.f1,backupdatabasename.dbo.table1.f2,backupdatabasename.dbo.table1.f3,..., basedatabasename.dbo.table1.f1 as newf1,basedatabasename.dbo.table1.f2 as newf2
from backupdatabasename.dbo.table1
left outer join basedatabasename.dbo.table1
on backupdatabasename.dbo.table1.f1=basedatabasename.dbo.table1.f1
and backupdatabasename.dbo.table1.f2=basedatabasename.dbo.table1.f2
) as t
where t.newf1 is null and t.newf2 is null
Random Solutions  
 
programming4us programming4us