AdoQuery1中保存有若干条记录,如下:
-------
原始数据
xh time
20001221 7:21
20001221 7:30
20001221 7:40学号(xh)字段相同,时间不同,有先后顺序,我要求对时间升序排序后对“学号”
字段进行更新,即时间最早的记录添加最多个“备注”字样,如下所示,只有时间最
近的记录保持原有的学号不变,如下所示:修改后应该为
==========================================
xh time
20001221备注备注 7:21
20001221备注 7:30
20001221 7:40代码如下:
while not AdoQuery1.Eof do
begin
strBZ:=strBZ+'备注';
AdoQuery1.Edit;
AdoQuery1.FieldByName('xh').AsString:=AdoQuery1.FieldByName('xh').AsString+strBZ;
AdoQuery1.ExecSQL;
AdoQuery1.Post;
AdoQuery1.Next;
end;
这段代码外面还有一层大循环,对整个表中每个重复学号都进行一遍扫描,直到没有重复学号为止,问题是如果某个学号重复最多两次可以,能够进行正常工作,如果3次或者3次以上就会报错!“键列信息不足或不正确,更新影响到多行”或者不报错,但是只更新三行中的两行。
-------
原始数据
xh time
20001221 7:21
20001221 7:30
20001221 7:40学号(xh)字段相同,时间不同,有先后顺序,我要求对时间升序排序后对“学号”
字段进行更新,即时间最早的记录添加最多个“备注”字样,如下所示,只有时间最
近的记录保持原有的学号不变,如下所示:修改后应该为
==========================================
xh time
20001221备注备注 7:21
20001221备注 7:30
20001221 7:40代码如下:
while not AdoQuery1.Eof do
begin
strBZ:=strBZ+'备注';
AdoQuery1.Edit;
AdoQuery1.FieldByName('xh').AsString:=AdoQuery1.FieldByName('xh').AsString+strBZ;
AdoQuery1.ExecSQL;
AdoQuery1.Post;
AdoQuery1.Next;
end;
这段代码外面还有一层大循环,对整个表中每个重复学号都进行一遍扫描,直到没有重复学号为止,问题是如果某个学号重复最多两次可以,能够进行正常工作,如果3次或者3次以上就会报错!“键列信息不足或不正确,更新影响到多行”或者不报错,但是只更新三行中的两行。
数据表不能修改!就说这段代码有什么错误?
var
n,i:integer;
strBZ,strTime,strXh:String;
TTime:TDateTime;
begin
if AdoQuery1.ConnectionString='' then
begin
AdoQuery1.ConnectionString:='DBQ='+strDBName+';Driver={Microsoft Access Driver (*.mdb)};';
AdoQuery2.ConnectionString:=AdoQuery1.ConnectionString;
end;
AdoQuery2.SQL.Clear;
AdoQuery2.SQL.Add('Select * from PicNum_temp order by total desc;');
/*PicNum_temp表中保存的是先前在别处由PicNum表统计生成的PicNum表中有重复学号的学生及这个学号重复的次数(total)*/
AdoQuery2.Active:=true;
AdoQuery2.First;
while not AdoQuery2.Eof do
begin
strXh:=AdoQuery2.FieldByName('xh').AsString;
n:=AdoQuery2.FieldByName('Total').AsInteger;
AdoQuery1.SQL.Clear;
//按时间降序排序,所以第一条记录就是正确的记录
AdoQuery1.SQL.Add('Select xh,myDate,mytime from PicNum where xh='''+strXh+''' order by myDate,mytime DESC;');
AdoQuery1.Active:=true;
AdoQuery1.First;
AdoQuery1.Next;
strBZ:='';
try
while not AdoQuery1.Eof do
begin
strBZ:=strBZ+'补照';
AdoQuery1.Edit;
AdoQuery1.FieldByName('xh').AsString:=AdoQuery1.FieldByName('xh').AsString+strBZ;
AdoQuery1.Post;
AdoQuery1.Next;
end;
except
end;
AdoQuery2.Next;
end;
end;
真正能解决我这个问题的哥们给60分,其余分平分.
/*PicNum_temp表中保存的是先前在别处由PicNum表统计生成的PicNum表中有重复学号的学生及这个学号重复的次数(total)*/
应该把 /*...*/改为 { }
注意:这不是错误!在真实程序中没有这个注释,这是在发贴子时自己新加的。
var
n,i:integer;
strBZ,strTime,strXh:String;
TTime:TDateTime;
begin
if AdoQuery1.ConnectionString='' then
begin
AdoQuery1.ConnectionString:='DBQ='+strDBName+';Driver={Microsoft Access Driver (*.mdb)};';
AdoQuery2.ConnectionString:=AdoQuery1.ConnectionString;
end;
AdoQuery2.SQL.Clear;
AdoQuery2.SQL.Add('Select * from PicNum_temp order by total desc;');
{PicNum_temp表中保存的是先前在别处由PicNum表统计生成的PicNum表中有重复学号的学生及这个学号重复的次数(total)}
AdoQuery2.Active:=true;
AdoQuery2.First;
while not AdoQuery2.Eof do
begin
strXh:=AdoQuery2.FieldByName('xh').AsString;
n:=AdoQuery2.FieldByName('Total').AsInteger;
AdoQuery1.SQL.Clear;
//按时间降序排序,所以第一条记录就是正确的记录
AdoQuery1.SQL.Add('Select xh,myDate,mytime from PicNum where xh='''+strXh+''' order by myDate,mytime DESC;');
AdoQuery1.Active:=true;
AdoQuery1.First;
AdoQuery1.Next;
strBZ:='';
try
while not AdoQuery1.Eof do
begin
strBZ:=strBZ+'补照';
AdoQuery1.Edit;
AdoQuery1.FieldByName('xh').AsString:=AdoQuery1.FieldByName('xh').AsString+strBZ;
AdoQuery1.Post;
AdoQuery1.Next;
end;
except
end;
AdoQuery2.Next;
end;
end;
AdoQuery1.Post;
AdoQuery1.Refresh;
,不行,问题一样!
能解决我这个问题的,分不够??可以再加分!200!
select distinc xh from PicNum就可以得到不重复的XH列表.
{PicNum_temp表中保存的是先前在别处由PicNum表统计生成的PicNum表中有重复学号的学生及这个学号重复的次数(total)}
AdoQuery2.Active:=true;
AdoQuery2.First;
while not AdoQuery2.Eof do
begin
strXh:=AdoQuery2.FieldByName('xh').AsString;
n:=AdoQuery2.FieldByName('Total').AsInteger;
AdoQuery1.SQL.Clear;
//按时间降序排序,所以第一条记录就是正确的记录
AdoQuery1.SQL.Add('Select xh,myDate,mytime from PicNum where xh='''+strXh+''' order by myDate,mytime DESC;');
AdoQuery1.Active:=true;
AdoQuery1.First;
//AdoQuery1.Next;////////////////////////////去掉此句
strBZ:='';
try
while not AdoQuery1.Eof do
begin
strBZ:=strBZ+'补照';
AdoQuery1.Edit;
AdoQuery1.FieldByName('xh').AsString:=AdoQuery1.FieldByName('xh').AsString+strBZ;
AdoQuery1.Post;
AdoQuery1.Next;
end;
还有就是我觉得你的SQL写成'Select xh,myDate,mytime from PicNum group by xh,mydate
order by mytime desc //adoquery1然后再写出每个学号有多少条记录 select xh ,count(xh) cxh from picnum group by xh //adoquery2
你也可以把两个adoquery的SQL 写成一个循环的时候就
var cxh,i:integer;
xh,str:string;
xh:=0;
adoquery1.first;
while not adoquery1.eof do
begin
//每遇到一个新的学号就处理一下,取记录数
if xh<>fieldbyname('xh').asstring then
begin
xh:=fieldbyname('xh').asstring;
if adoquery2.locate('xh',xh,[]) then
cxh:=adoquery2.fieldbyName('cxh').asinteger;
end;
str:='';
for i:=0 to cxh-1 do
str:=str+'备注'; fieldbyname('xh').asstring:=fieldbyname('xh').asstring+str; dec(cxh);//下次循环少一次 post;
next;
end;
end;因为是在网页上面写的,所以没有经过最基本的语法校验,有错误请自行改正,给一个大概思路出来,供参考。
//AdoQuery1.Next;////////////////////////////去掉此句
这句没错,这是我有意添加的,意思是按时间降序排列后不是从第一条记录进行更新,而是从第二条记录开始更新。