22,210
社区成员
发帖
与我相关
我的任务
分享
set transaction isolation level snapshot
select * from tb_name
select status,count(*)
from account
where time >='2009-03-30 11:00' and time<'2009-03-30 12:00'
group by status;
select status,count(*)
from account
where time ='2009-03-30 11:00'
group by status;
-- 基本思路:用 [timestamp] 和 [deleted] 列跟踪老数据;用 update 语句执行删除操作;用 update、insert 语句执行更新操作
if OBJECT_ID('tab') is not null
drop table tab;
go
create table tab
(id int, c char(1),
[deleted] bit default 0,
[timestamp] datetime default getdate()
);
go
-- insert rows
insert into tab (id,c) values(1,'a'),(2,'b'),(3,'c');
go
-- delete rows (delete from tab where id=3;)
update tab set deleted=1 where id=3;
go
-- update rows (update tab set c='x' where id=2)
begin tran
update tab set deleted=1 where id=2;
insert into tab (id,c,[timestamp])
select id,'x',SYSDATETIME() from tab where id=2
commit tran
go
-- query (select * from tab;)
select id,c from tab where deleted=0;
-- query (select * from tab as of timestamp 'yyyy-mm-dd hh:mi:ss')
;with t as(
select * from tab t where [timestamp]<='yyyy-mm-dd hh:mi:ss' )
select id, c from t t1 where
not exists (select * from t where t1.id=id and [timestamp]>t1.[timestamp]);
-- clear old snapshot
delete from tab where deleted=1;