按扭事件如下:procedure Tform2.Button4Click(Sender: TObject);
begin
button3.enabled:=false;
groupbox5.visible:=true;
groupbox4.visible:=false;
sqlquery1.close;
sqlquery1.sql.clear;
sqlquery1.sql.text :='select 学号,姓名, sum(成绩 * 学分)/sum(学分) as 智育,min(成绩) as 最低分,学期,年份 from '+flag+' where 年份='+edit1.text+' and 学期='+QuotedSTR(edit2.text)+' and 类别 <> '+ Quotedstr('体育')+' and 类别 <> '+quotedstr('德育')+' group by 年份,学期,学号,姓名 order by 智育 desc ';
sqlquery1.open;
clientdataset1.close;
clientdataset1.open;
end;这样在dbgrid中是能显示出我需要的结果了,但是我想把这个结果保存到"智育"表中,改成这样的代码:procedure Tform2.Button4Click(Sender: TObject);
begin
button3.enabled:=false;
groupbox5.visible:=true;
groupbox4.visible:=false;
sqlquery1.close;
sqlquery1.sql.clear;
sqlquery1.sql.text :='insert into 智育 select 学号,姓名, sum(成绩 * 学分)/sum(学分) as 智育,min(成绩) as 最低分,学期,年份 from '+flag+' where 年份='+edit1.text+' and 学期='+QuotedSTR(edit2.text)+' and 类别 <> '+ Quotedstr('体育')+' and 类别 <> '+quotedstr('德育')+' group by 年份,学期,学号,姓名 order by 智育 desc ';
//sqlquery1.open;
sqlquery1.ExecSQL();
clientdataset1.close;
sqlquery1.close;
sqlquery1.sql.text:='select * from 智育 ';
sqlquery1.open;
clientdataset1.open;
end;为什么不行 呢?我是新手,还不太懂,请大家帮我看看该如何实现.
begin
button3.enabled:=false;
groupbox5.visible:=true;
groupbox4.visible:=false;
sqlquery1.close;
sqlquery1.sql.clear;
sqlquery1.sql.text :='select 学号,姓名, sum(成绩 * 学分)/sum(学分) as 智育,min(成绩) as 最低分,学期,年份 from '+flag+' where 年份='+edit1.text+' and 学期='+QuotedSTR(edit2.text)+' and 类别 <> '+ Quotedstr('体育')+' and 类别 <> '+quotedstr('德育')+' group by 年份,学期,学号,姓名 order by 智育 desc ';
sqlquery1.open;
clientdataset1.close;
clientdataset1.open;
end;这样在dbgrid中是能显示出我需要的结果了,但是我想把这个结果保存到"智育"表中,改成这样的代码:procedure Tform2.Button4Click(Sender: TObject);
begin
button3.enabled:=false;
groupbox5.visible:=true;
groupbox4.visible:=false;
sqlquery1.close;
sqlquery1.sql.clear;
sqlquery1.sql.text :='insert into 智育 select 学号,姓名, sum(成绩 * 学分)/sum(学分) as 智育,min(成绩) as 最低分,学期,年份 from '+flag+' where 年份='+edit1.text+' and 学期='+QuotedSTR(edit2.text)+' and 类别 <> '+ Quotedstr('体育')+' and 类别 <> '+quotedstr('德育')+' group by 年份,学期,学号,姓名 order by 智育 desc ';
//sqlquery1.open;
sqlquery1.ExecSQL();
clientdataset1.close;
sqlquery1.close;
sqlquery1.sql.text:='select * from 智育 ';
sqlquery1.open;
clientdataset1.open;
end;为什么不行 呢?我是新手,还不太懂,请大家帮我看看该如何实现.
那个语句我在sql 里面运行是可以的啊还有我按照你说的加 了字段名也是不行哦
clientdataset1.close;//关闭数据库,下面的sqlquery1.open会出错!
sqlquery1.close;
sqlquery1.sql.text:='select * from 智育 ';
sqlquery1.open;
clientdataset1.open;
要是还不行,你把错误信息贴上来看看!^0^
procedure Tform2.Button4Click(Sender: TObject);
begin
button3.enabled:=false;
groupbox5.visible:=true;
groupbox4.visible:=false;
sqlquery1.close;
sqlquery1.sql.clear;
sqlquery1.sql.add('select 学号,姓名, sum(成绩 * 学分)/sum(学分) as 智育,min(成绩) as 最低分,学期,年份 into 智育(学号,姓名,智育,最低分,学期,年份) from '+flag+' where 年份='+edit1.text+' and 学期='+QuotedSTR(edit2.text)+' and 类别 <> '+ Quotedstr('体育')+' and 类别 <> '+quotedstr('德育')+' group by 年份,学期,学号,姓名 order by 智育 desc ');
//sqlquery1.open;
//sqlquery1.ExecSQL();
//sqlquery1.close;
//sqlquery1.sql.add('select * from 智育 ');
sqlquery1.open;
clientdataset1.close;
clientdataset1.open;
end;
但是还是有出错消息,哪里不对吗?大家帮我看看啊.谢谢了..
'ClientDataSet1: Field '最低分' not found'.Process stopped.Use Step or Run to continue.
楼主这样写语法是没错的,但是有几点注意一下
1>您的年份字段是不是数值型,如果不是,请改为 年份='''+edit1.text+''' and 学期
2>您的目标表的结构勿必要有与源表查询结果完全一样的字段列表
2>您的目标表的结构勿必要有与源表查询结果完全一样的字段列表 也是符合的
但是就是出错,不知道错哪里了.我现在改了一些,但是还是不行.
procedure Tform2.Button4Click(Sender: TObject);
begin
button3.enabled:=false;
groupbox5.visible:=true;
groupbox4.visible:=false;
clientdataset1.close;
sqlquery1.close;
sqlquery1.sql.clear;
sqlquery1.sql.text:='select 学号,姓名, sum(成绩 * 学分)/sum(学分) as 智育,min(成绩) as 最低分,学期,年份 from '+flag+' where 年份='+edit1.text+' and 学期='+QuotedSTR(edit2.text)+' and 类别 <> '+ Quotedstr('体育')+' and 类别 <> '+quotedstr('德育')+' group by 年份,学期,学号,姓名 order by 智育 desc';
sqlquery1.open;
clientdataset1.Open;
clientdataset1.first;
while not clientdataset1.Eof do
begin
sqlquery1.close;
sqlquery1.sql.clear;
sqlquery1.sql.text:='select * from 智育 where 学号='+Quotedstr(clientdataset1.fieldbyname('学号').asstring)+' and 年份='+edit1.text+' and 学期='+QuotedSTR(edit2.text)+' ';
sqlquery1.open;
if Sqlquery1.RecordCount=0 then
begin
sqlquery1.close;
sqlquery1.sql.clear;
sqlquery1.sql.text:='insert into 智育 values('+Quotedstr(clientdataset1.fieldbyname('学号').asstring)+','+Quotedstr(clientdataset1.fieldbyname('姓名').asstring)+','+floatToStr(clientdataset1.fieldbyname('智育').asfloat)+','+floatToStr(clientdataset1.fieldbyname('最低分').asfloat)+','+Quotedstr(clientdataset1.fieldbyname('学期').asstring)+','+IntToStr(clientdataset1.fieldbyname('年份').asInteger)+')';
showmessage(sqlquery1.sql.text);
sqlquery1.ExecSQL();
sqlquery1.open;
clientdataset1.next;
end;
clientdataset1.next;
end;
clientdataset1.close;
sqlquery1.close;
sqlquery1.sql.text:='select * from 智育 where 年份='+edit1.text+' and 学期='+QuotedSTR(edit2.text)+' order by 智育 desc';
sqlquery1.open;
clientdataset1.open;
end;
procedure Tform2.Button4Click(Sender: TObject);
begin
button3.enabled:=false;
groupbox5.visible:=true;
groupbox4.visible:=false;
clientdataset1.close;
sqlquery1.close;
sqlquery1.sql.clear;
sqlquery1.sql.text:='select 学号,姓名, sum(成绩 * 学分)/sum(学分) as 智育,min(成绩) as 最低分,学期,年份 from '+flag+' where 年份='+edit1.text+' and 学期='+QuotedSTR(edit2.text)+' and 类别 <> '+ Quotedstr('体育')+' and 类别 <> '+quotedstr('德育')+' group by 年份,学期,学号,姓名 order by 智育 desc';
showmessage(sqlquery1.sql.text);
sqlquery1.open;
clientdataset1.Open;
clientdataset1.first;
while not clientdataset1.Eof do
begin
sqlquery1.close;
sqlquery1.sql.clear;
sqlquery1.sql.text:='select * from 智育 where 学号='+Quotedstr(clientdataset1.fieldbyname('学号').asstring)+' and 年份='+edit1.text+' and 学期='+QuotedSTR(edit2.text)+' ';
showmessage(sqlquery1.sql.text);
sqlquery1.open;
if Sqlquery1.RecordCount=0 then
begin
sqlquery1.close;
sqlquery1.sql.clear;
sqlquery1.sql.text:='insert into 智育 values('+Quotedstr(clientdataset1.fieldbyname('学号').asstring)+','+Quotedstr(clientdataset1.fieldbyname('姓名').asstring)+','+floatToStr(clientdataset1.fieldbyname('智育').asfloat)+','+floatToStr(clientdataset1.fieldbyname('最低分').asfloat)+','+Quotedstr(clientdataset1.fieldbyname('学期').asstring)+','+IntToStr(clientdataset1.fieldbyname('年份').asInteger)+')';
showmessage(sqlquery1.sql.text);
sqlquery1.ExecSQL();
end;
clientdataset1.next;
end;
clientdataset1.close;
sqlquery1.close;
sqlquery1.sql.text:='select * from 智育 where 年份='+edit1.text+' and 学期='+QuotedSTR(edit2.text)+' order by 智育 desc';
showmessage(sqlquery1.sql.text);
sqlquery1.open;
clientdataset1.open;
end;