你觉得这样看行不行,再用一个表当创建了table2时,把table1里的记录备份一下,也就是说,这个表专用来备份table1记录作了哪些修改,如果当table1记录有新增,或者修改时,就和这个表里的记录比较一下,如果发现有不同了,就更新table2里的列和结构,
假设:
table1
//-------------------------------
FieldName FieldType Length
ID号 varchar 15
UnitName varchar 10
Qty float 8
----------------------------------
//table2 表结构
ID号 UnitName Qty
01-01 KG 1.85
01-02 KG 2.47
. . .
. . .
. . .
01-15 KG 100
//------另加一个表和table1记录一样,每次新增也要往这个表加记录,但不能修改这个表记录
简写代码如下:要放在某个事件执行:
//---------
//---ttempa 备份表
var
FieldList,OldFieldList:String ;
Str:String ;
Query1.Close
Query1.SQL.Clear;
Query1.SQL.Add('Select * from table1 where FieldName not in (select FieldName from ttempa)');
Query1.Open
//==========找出列来,把table2新增列
FieldList:=''
if not Query1.IsEmpty then
begin
Query1.First ;
while not Query1.Eof then
begin
if (UPPERCASE(Trim(Query1.FieldByName('FieldType').AsString))<>'CHAR')
OR (UPPERCASE(Trim(Query1.FieldByName('FieldType').AsString))<>'VARCHAR')
OR (UPPERCASE(Trim(Query1.FieldByName('FieldType').AsString))<>'TEXT') then
begin
FieldList:= FieldList +','+Query1.FieldByName('FieldName').AsString
+Query1.FieldByName('FieldType').AsString+'('+Query1.FieldByName('Length').AsString+')') ;
end
else if (UPPERCASE(Trim(Query1.FieldByName('FieldType').AsString))<>'BIT')
OR(UPPERCASE(Trim(Query1.FieldByName('FieldType').AsString))<>'INT')
OR(UPPERCASE(Trim(Query1.FieldByName('FieldType').AsString))<>'FLOAT')
OR(UPPERCASE(Trim(Query1.FieldByName('FieldType').AsString))<>'DATETIME')then
begin
FieldList:= FieldList +','+Query1.FieldByName('FieldName').AsString
+Query1.FieldByName('FieldType').AsString) ;
end ;
Query1.Next ;
end ;
FieldList:=Copy(FieldList,2,length(FieldList));
Str:= 'Alter table2 Add '+FieldList ;//新增列
QueryUpdate.Close ;
QueryUpdate.SQL.Clear ;
Str:= 'Alter table2 Add '+FieldList ;
QueryUpdate.SQL.Add(Str);
QueryUpdate.ExecSQL;
end
else
begin
//======表明table1没有加记录,进行比较,如果不同,即要修改table2的列
QueryOld.Close
QueryOld.SQL.Clear;
QueryOld.SQL.Add('select FieldName from ttempa');
QueryOld.Open
OldFieldList:='' ;
Query1.First ;
while not Query1.Eof then
begin
if (Query1.FieldByName('FieldByName').AsString<> QueryOld.FieldByName('FieldByName').AsString)
AND (Query1.FieldByName('FieldType').AsString<> QueryOld.FieldByName('FieldType').AsString)
AND (Query1.FieldByName('Length').AsString<> QueryOld.FieldByName('Length').AsString)then
begin
if (UPPERCASE(Trim(Query1.FieldByName('FieldType').AsString))<>'CHAR')
OR (UPPERCASE(Trim(Query1.FieldByName('FieldType').AsString))<>'VARCHAR')
OR (UPPERCASE(Trim(Query1.FieldByName('FieldType').AsString))<>'TEXT') then
begin
FieldList:= FieldList +','+Query1.FieldByName('FieldName').AsString
+Query1.FieldByName('FieldType').AsString+'('+Query1.FieldByName('Length').AsString+')') ;
end
else if (UPPERCASE(Trim(Query1.FieldByName('FieldType').AsString))<>'BIT')
OR(UPPERCASE(Trim(Query1.FieldByName('FieldType').AsString))<>'INT')
OR(UPPERCASE(Trim(Query1.FieldByName('FieldType').AsString))<>'FLOAT')
OR(UPPERCASE(Trim(Query1.FieldByName('FieldType').AsString))<>'DATETIME')then
begin
FieldList:= FieldList +','+Query1.FieldByName('FieldName').AsString
+Query1.FieldByName('FieldType').AsString) ;
end ;
//这个用来当修改Table2 列时,把原有的列删除
OldFieldList:=OldFieldList+','+Query1.FieldByName('FieldName').AsString;
end ;
Query1.Next ;
end ;
FieldList:=Copy(FieldList,2,length(FieldList));
//=========先删除要修改table2的列
QueryUpdate.Close ;
QueryUpdate.SQL.Clear ;
Str:= 'Alter table2 Drop Column '+OldFieldList ;
QueryUpdate.SQL.Add(Str);
QueryUpdate.ExecSQL;
//=========同时也把备份表中记录再重新从table1中导入一次,或者只把不同的记录导入就可以
//=====这里不介绍了
//=========在Tabel2加入新列
Str:= 'Alter table2 Add '+FieldList ;//加入新列
QueryUpdate.Close ;
QueryUpdate.SQL.Clear ;
Str:= 'Alter table2 Add '+FieldList ;
QueryUpdate.SQL.Add(Str);
QueryUpdate.ExecSQL;
end ;
//=====当修改table2列或者新列之后,再把记录从别的相关表导入或者更新就可以,这里不作介绍了
上述代码,你可以参考一下