declare @a int,@b int,@c int
select @a=sum(Amount) from stocks where InDate<'2008/9/5'
select @b=sum(TotalPrices) from stocks where InDate>='2008/9/5' and InDate<='2008/9/30'
set @c=@a+@b
select @c
能否将以上SQL语句转换成delphi里面的格式~感谢~

解决方案 »

  1.   

    var
      a, b, c: Integer;  function GetValue(ASQL: string): Integer;
      begin
        With TADODataSet.Create(nil) do
        begin
          Connection := YourADOConn;
          CommandText := ASQL;
          Open;
          Result := Fields[0].Value
          Free;
        end;
      end;begin
      a := GetValue('select sum(Amount) from stocks where InDate<''2008/9/5''');
      b := GetValue('select sum(TotalPrices) from stocks where InDate>=''2008/9/5'' and InDate<=''2008/9/30''');
      c := a+b;
      ShowMessage(IntToStr(c));
    end;
      

  2.   

    var 
      a, b, c: Integer;   function GetValue(ASQL: string): Integer; 
      begin 
        With TADODataSet.Create(nil) do 
        begin 
          Connection := YourADOConn; 
          CommandText := ASQL; 
          Open; 
          Result := Fields[0].Value 
          Free; 
        end; 
      end; begin 
      a := GetValue('select sum(Amount) from stocks where InDate <''2008/9/5'''); 
      b := GetValue('select sum(TotalPrices) from stocks where InDate>=''2008/9/5'' and InDate <=''2008/9/30'''); 
      c := a+b; 
      ShowMessage(IntToStr(c)); 
    end;试一下
      

  3.   


    var
      a,b,c: float;
    begin
      // other code........
      adoquery1.sql.txt:= 'select sum(Amount) from stocks where InDate < '''+ the date + '''';
      adoquery1.execsql;
      a:= adoquery1.Fields.Fields[0].asfloat;
      adoquery1.sql.txt:= 'select sum(TotalPrices) from stocks where InDate>= ''' + begin date +
                          'and InDate<= ''' + end date + '''';
      adoquery1.execsql;
      b:= adoquery1.Fields.Fields[0].asfloat;
      c:= a + b;
      adoquery1.free;
    end
      

  4.   


    var
      s : widestring;
      adoquery1 : tadoquery;
    begin
      s := ' declare @a int,@b int,@c int'
         + ' select @a=sum(Amount) from stocks where InDate<''2008/9/5'''
         + ' select @b=sum(TotalPrices) from stocks where InDate>=''2008/9/5'' and InDate<=''2008/9/30'''
         + ' set @c=@a+@b'
         + ' select @c';
      //自己定义adoquery1 及其连接
      with adoquery1 do
      begin
        sql.Text := s;
        open;
        showmessage(fields.Fields[0].AsString);
      end;
    end;
      

  5.   

    这样好像不行,会提示无效的SQL语句,期待'Delete'、'Insert'、'PROCEDURE'、'Select'OR'Update',可能
    'declare @a int,@b int,@c int'这句的关系,有点郁闷,还是非常感谢~
      

  6.   

    我不好意思才是,忘记在begin date后面加引号,害你要修改