我认为这就是你需要的 -
declare @temp_20130427 table (
PID INT
,SID INT
,Relationship char(1)
,Lname varchar(50)
,Fname varchar(50)
,Col1 varchar(50)
,PII varchar(50)
,LastUpdated datetime
)
insert into @temp_20130427 values (1,1,'P','Khan','Shahrukh','Actor','Famous',GETDATE())
,(2,2,'P','Khan','Salman','Actor','Famous',GETDATE())
,(3,3,'P','Khan','Saif Ali','Actor','Famous',GETDATE())
select * from @temp_20130427
declare @temp_20130426 table (
PID INT
,SID INT
,Relationship char(1)
,Lname varchar(50)
,Fname varchar(50)
,Col1 varchar(50)
,PII varchar(50)
,LastUpdated datetime
,[Delete] bit
)
insert into @temp_20130426 values (1,1,'P','Khan','Shahrukh','Actor','Famous',GETDATE(),NULL)
,(2,2,'P','Khan','Salman','Actor','Famous',GETDATE(),NULL)
,(3,3,'P','Khan','Saif Ali','Actor','Famous',GETDATE(),NULL)
,(4,4,'P','Kumar','Akshay','Actor','Famous',GETDATE(),NULL)
select * from @temp_20130426
--Insert the compared data in a new #tmp table
IF OBJECT_ID ('tempdb..#tmp') is not null
DROP TABLE #tmp
select PID
,SID
,Relationship
,Lname
,Fname
,Col1
,PII
INTO #tmp
from @temp_20130426
EXCEPT
select PID
,SID
,Relationship
,Lname
,Fname
,Col1
,PII
from @temp_20130427
IF OBJECT_ID('TEMPDB..#NEW_TABLE') IS NOT NULL
DROP TABLE #NEW_TABLE
select told.PID
,told.SID
,told.Relationship
,told.Lname
,told.Fname
,told.Col1
,told.PII
,told.LastUpdated
INTO #NEW_TABLE
from #tmp t
join @temp_20130426 tOld
on tOld.PID = t.PID
--See the different data set in #NEW_TABLE table
SELECT * FROM #NEW_TABLE
--Flag Delete column where record does not exist in next day table
update t
set t.[Delete] = 1
from @temp_20130426 t
where PID not in (select PID from @temp_20130427)
--See the updated flagged data
select * from @temp_20130426