我创建了一个临时表#tbl(account, last_update)。我有两个来自不同来源(可能是来自不同数据库的表)的插入以插入具有最后更新日期的帐户。例如
create table #tbl ([account] numeric(18, 0), [last_update] datetime)
insert into #tbl(account , last_update)
select table1.account, max(table1.last_update)
from table1 join…
group by table1.account
insert into #tbl(account , last_update)
select table2.account, max(table2.last_update)
from table2 join…
group by table2.account
问题是这可能会导致表#tbl 中的帐户重复。我要么必须在每次插入期间避免它,要么在两次插入后删除重复项。此外,如果有两个不同的 last_update 帐户,我希望 #tbl 具有最新的 last_update。我如何实现这个条件插入?哪一个会有更好的表现?