按扭事件如下: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;为什么不行 呢?我是新手,还不太懂,请大家帮我看看该如何实现.

解决方案 »

  1.   

    'insert into 智育(【这儿需要增加字段列表】) select 【这儿的字段列表要和前面的相同,如果字段名不同的话,则用as设置相同】
      

  2.   

    请问字段都一样也要增加字段名么?
    那个语句我在sql 里面运行是可以的啊还有我按照你说的加 了字段名也是不行哦
      

  3.   

    sqlquery1.ExecSQL();
       clientdataset1.close;//关闭数据库,下面的sqlquery1.open会出错!
       sqlquery1.close;
       sqlquery1.sql.text:='select * from 智育 ';
       sqlquery1.open;
       clientdataset1.open;
    要是还不行,你把错误信息贴上来看看!^0^
      

  4.   

    还有,你的插入SQL语句有问题,要修改成楼上“小虫”给出的那样!
      

  5.   

    select * into destination_table_name from source_table_name where XXXXX
      

  6.   

    使用select into 语句,例子如楼上,速度很快的。
      

  7.   

    我按照楼上几位说的改成这样子了:
    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;
    但是还是有出错消息,哪里不对吗?大家帮我看看啊.谢谢了..
      

  8.   

    错误信息图片发不上来.我大概写一下:project project1.exe raised exception class EDatabaseError with message
    'ClientDataSet1: Field '最低分' not found'.Process stopped.Use Step or Run to continue.
      

  9.   

    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 ';
    楼主这样写语法是没错的,但是有几点注意一下
    1>您的年份字段是不是数值型,如果不是,请改为 年份='''+edit1.text+''' and 学期
    2>您的目标表的结构勿必要有与源表查询结果完全一样的字段列表
      

  10.   

    年份是int型的,还有
    2>您的目标表的结构勿必要有与源表查询结果完全一样的字段列表  也是符合的
    但是就是出错,不知道错哪里了.我现在改了一些,但是还是不行.
      

  11.   

    我把程序改成这样后,出错还是照样有,但是记录是能插入一条进数据库了,但是也不显示出来,因为没有到显示那条语句的时候就已经出错了.
    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;
      

  12.   

    经过同学的帮助,问题已经解决了,谢谢大家!
    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;