很多时候,都需要对数据表进行历史记录。比如每修改一次表单,之前的表单数据都需要计入历史。当表单比较多的时候,记录历史是一件比较麻烦的事情。又要建日志表,又要写存储过程,又要写页面逻辑等等。有没有通用点的办法呢?最近做项目时碰到了,要求每次审核、退回等操作时就要记录表历史。于是,笔者就想到了以下方案。在此与大家分享了,如果有更合适的或合理的建议,请回复本帖。

1)创建日志表

一个一个建表是一件烦躁的事,而且还容易出错。那么,以下存储过程就能批量建表了,还添加了LogCreateDate、LogDefaultFlag、LogPTID这3个字段。值得注意的是,创建表结构可以用以下语句“SELECT * Into tableName_Log FROM tableName”。如果只需要复制表结构,那就插入一行,再删除就是。

SQL里面实现遍历数据集不方便,不想用游标,于是采用了以下方式。具体存储过程如下:

01 USE [NbShop]
02 GO
03 /****** Object:  StoredProcedure [dbo].[CreateLogTable]    Script Date: 07/02/2011 12:54:32 ******/
04 SET ANSI_NULLS ON
05 GO
06 SET QUOTED_IDENTIFIER ON
07 GO
08  
09  
10 -- =============================================
11 -- Author:      LWQ
12 -- Create date: 2011-6-29
13 -- Description: 创建日志表(命名规则:表名+_Log)
14 -- =============================================
15 ALTER PROCEDURE [dbo].[CreateLogTable]
16 AS
17 BEGIN
18     -- SET NOCOUNT ON added to prevent extra result sets from
19     -- interfering with SELECT statements.
20     SET NOCOUNT ON;
21  
22     -- Insert statements for procedure here
23     -------------------创建日志表------------------------------
24 declare @rows     int
25 declare @n        int
26 declare @tableName        varchar(100)
27 select @n=1
28     SELECT     name
29     INTO            [#tempTables]
30     FROM         sys.sysobjects
31     WHERE     (xtype = 'U 'AND (name NOT IN ('sysdiagrams''T_BasicTime''T_Attribute''T_AttributeType''T_BasicTime''T_City','T_CompeteForMeasu',
32                           'T_DocumentTypeRestrictions''T_FormRelevance''T_HistroyShopAction''T_Notice''T_NoticeReceive''T_Organize''T_OrgType',
33                           'T_Province''T_Role''T_RptShopStatus''T_UploadFile''T_UrlPrint'))
34                           AND (name NOT LIKE '%flow%'AND (name NOT LIKE '%Control%'AND
35                           (name NOT LIKE '%Menu%'AND (name NOT LIKE '%Node%'AND (name NOT LIKE '%Log%'AND (name NOT LIKE '%Event%'AND (name NOT LIKE '%Object%'AND
36                           (name NOT LIKE '%Process%'AND (name NOT LIKE '%ShopStatus%'AND (name NOTLIKE '%Task%')
37                           AND (name NOT LIKE '%ThirdParty%'AND (name NOT LIKE '%User%')
38                           AND (name NOT LIKE '%order%')
39      
40     Select from   #tempTables
41     Select name into #tempCurrent  from #tempTables
42     Delete from  #tempCurrent
43  
44      select @rows = @@rowcount
45     while @n <= @rows
46     begin
47       set @tableName=(Select  top 1  name from #tempTables
48       Where name not in
49       (select name from #tempCurrent))
50       if(@tableName is not null)
51       begin
52         insert into #tempCurrent values(@tableName)
53         if object_id(@tableName+'_Log'is not null
54         begin
55             print   '表'+  @tableName +'已存在,仅做数据更新处理'
56             exec ('INSERT INTO'+ @tableName +'_Log SELECT * FROM '+@tableName)             
57         end
58         else
59         begin
60             exec ('SELECT * Into '+@tableName+'_Log FROM '+@tableName)
61             print   '表'+  @tableName +'创建成功'
62             exec ('alter   table   '+@tableName+'_Log   add   LogCreateDate   datetime')
63             exec ('alter   table   '+@tableName+'_Log   add   LogDefaultFlag   int')
64             exec ('alter   table   '+@tableName+'_Log   add   LogPTID   varchar(32)')
65 ----            if   col_length( @tableName+' ',   'LogCreateDate ')   is not   null
66 ----            begin
67 ----                exec ('ALTER   TABLE   '+@tableName+'   DROP   COLUMN   LogCreateDate')
68 ----                print '删除'+@tableName+'的列LogCreateDate成功'
69 ----            end
70 ----            if(@tableName not in ('T_Shop','T_MeasurementAddress','T_TurnAround','T_IrisInstrumentHistory','T_ChainTurnApplication','T_TrainingNotice'))
71 ----            begin
72 ----                if   col_length( @tableName+' ',   'CreateDate ')   is not   null
73 ----                begin
74 ----                    exec ('ALTER   TABLE   '+@tableName+'   DROP   COLUMN   CreateDate ')
75 ----                    print '删除'+@tableName+'的列CreateDate成功'
76 ----                end
77 ----            end
78         end
79       end
80         select @n = @n + 1
81     end
82     drop table  #tempCurrent
83     drop table  #tempTables
84 -------------------创建日志表------------------------------
85 END
1 <br>

相关文章: