可记录历史数据的表单设计

格式

id

Record

Data

Created

Replaced

1

2

07/01/01 00:00

07/01/01 10:00

2

2

07/01/01 10:00

07/01/01 10:05

3

2

07/01/01 10:05

07/01/01 10:30

4

3

07/01/01 00:00

07/01/01 00:01

5

3

07/01/01 00:01

07/02/01 00:00

6

2

07/01/01 10:30

07/01/11 10:35

7

3

07/02/01 00:00

 

8

4

07/03/01 00:00

 

 
每次修改都往表中增加一行。

操作

新增记录比如4。很直观。

修改记录:写当前的记录Replaced时间戳,插入新记录Created时间戳。

取得记录:直接拿到Replaced时间为null的记录。

删除记录:写当前的记录Replaced时间戳。

取得删除的Records:

 
IF   EXISTS  ( SELECT   *   FROM  sysobjects  WHERE  name = ' #temp ' )
BEGIN
 
drop   table  # temp
END
GO
 
select  RowNum  =   IDENTITY ( int 1 1 ), id = ( [ db_id ] + 0 ), contact_id, dateReplaced  into  # temp   from  Contact  where  contact_id  in
(
 
select   distinct  contact_id  from  Contact a
 
where   not   exists  
 (
 
select   1   from  Contact 
 
where  contact_id = a.contact_id  and  DateReplaced  is   null
 )
)
order   by  contact_id, dateReplaced
 
select  id  from  # temp   where  RowNum  in
(
 
select   max (RowNum)  from  # temp
 
group   by  contact_id
)
 
drop   table  # temp


其中关键部分是删除的记录号

  select   distinct  contact_id  from  Contact a
 
where   not   exists  
 (
 
select   1   from  Contact 
 
where  contact_id = a.contact_id  and  DateReplaced  is   null
 )


总结

其他还有很多记录历史数据的方法。这只是其中之一。首推trigger+backup表。对于结构比较固定的表,推荐采用backup表。而此文介绍的方法对于维护要求很低。相对来说,在建立Stored Procedure上要多花很多时间来测试。可谓各有所长。

你可能感兴趣的:(测试,table,null,Go)