我有一个Staging和Master表,其中包含这样的列 第1步:
Create table Staging(
ID varchar(10) not null,
name varchar(100) not null,
createddate datetime not null
)
第2步:
insert into Staging('1','Amy','2014-01-01');
第3步:
Create table Master(
ID varchar(10) not null,
name varchar(100) not null,
createddate datetime not null,
verifieddate datetime not null
)
第4步:
insert into master(
select id,
name,
createddate,
createddate
from staging s
left outer join master on m on m.id=s.id where m.id is null);
第2天:
delete from Staging;
insert into Staging('1','Amy','2014-02-01');
insert into Staging('2','Binny','2014-01-01');
我运行第4步,将ID = 2插入Master。我想通过保持createddate相同来更新ID = 1,但验证日期应更改为2014-02-01。 所以,我写了这样的更新声明
update Master set VerifiedDate =
(case when i.verifieddate < a.createddate then a.createddate end)
FROM Staging a inner join
Master i
on i.id=a.[ID] where i.verifieddate < a.createddate
它不起作用。任何建议。
答案 0 :(得分:2)
理论上,您应该能够使用UPDATE
语句来执行此操作,如:
UPDATE m
SET VerifiedDate = s.CreatedDate
FROM Master AS m
INNER JOIN Staging AS s
ON m.Id = s.Id
AND s.CreatedDate > m.VerifiedDate