很多时候,都需要对数据表进行历史记录。比如每修改一次表单,之前的表单数据都需要计入历史。当表单比较多的时候,记录历史是一件比较麻烦的事情。又要建日志表,又要写存储过程,又要写页面逻辑等等。有没有通用点的办法呢?最近做项目时碰到了,要求每次审核、退回等操作时就要记录表历史。于是,笔者就想到了以下方案。在此与大家分享了,如果有更合适的或合理的建议,请回复本帖。
1)创建日志表
一个一个建表是一件烦躁的事,而且还容易出错。那么,以下存储过程就能批量建表了,还添加了LogCreateDate、LogDefaultFlag、LogPTID这3个字段。值得注意的是,创建表结构可以用以下语句“SELECT * Into tableName_Log FROM tableName”。如果只需要复制表结构,那就插入一行,再删除就是。
SQL里面实现遍历数据集不方便,不想用游标,于是采用了以下方式。具体存储过程如下:
03 |
/****** Object: StoredProcedure [dbo].[CreateLogTable] Script Date: 07/02/2011 12:54:32 ******/
|
06 |
SET QUOTED_IDENTIFIER ON
|
10 |
-- ============================================= |
12 |
-- Create date: 2011-6-29 |
13 |
-- Description: 创建日志表(命名规则:表名+_Log) |
14 |
-- ============================================= |
15 |
ALTER PROCEDURE [dbo].[CreateLogTable]
|
18 |
-- SET NOCOUNT ON added to prevent extra result sets from
|
19 |
-- interfering with SELECT statements.
|
22 |
-- Insert statements for procedure here
|
23 |
-------------------创建日志表------------------------------
|
26 |
declare @tableName varchar(100)
|
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%')
|
40 |
Select * from #tempTables
|
41 |
Select name into #tempCurrent from #tempTables
|
42 |
Delete from #tempCurrent
|
44 |
select @rows = @@rowcount
|
47 |
set @tableName=(Select top 1 name from #tempTables
|
49 |
(select name from #tempCurrent))
|
50 |
if(@tableName is not null)
|
52 |
insert into #tempCurrent values(@tableName)
|
53 |
if object_id(@tableName+'_Log') is not null
|
55 |
print '表'+ @tableName +'已存在,仅做数据更新处理'
|
56 |
exec ('INSERT INTO'+ @tableName +'_Log SELECT * FROM '+@tableName)
|
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 |
67 |
---- exec ('ALTER TABLE '+@tableName+' DROP COLUMN LogCreateDate') |
68 |
---- print '删除'+@tableName+'的列LogCreateDate成功' |
70 |
---- if(@tableName not in ('T_Shop','T_MeasurementAddress','T_TurnAround','T_IrisInstrumentHistory','T_ChainTurnApplication','T_TrainingNotice')) |
72 |
---- if col_length( @tableName+' ', 'CreateDate ') is not null |
74 |
---- exec ('ALTER TABLE '+@tableName+' DROP COLUMN CreateDate ') |
75 |
---- print '删除'+@tableName+'的列CreateDate成功' |
82 |
drop table #tempCurrent
|
83 |
drop table #tempTables
|
84 |
-------------------创建日志表------------------------------ |