如何从SQLServer获得所有数据

时间:2018-11-12 09:56:37

标签: sql sql-server database delphi ado

我正在尝试使用此请求通过链接到我的数据库的TADOQUERY从SQLSERVER中获取数据

declare @num_p varchar(9)
declare cur1 cursor for select do_piece from F_DOCENTETE where (DO_Piece like ('____00')or DO_Piece like ('____A0')or DO_Piece like ('____B0') 
or DO_Piece like ('____B0')or DO_Piece like ('____C0') OR DO_Piece like ('____D0')OR DO_Piece like ('____E0')OR DO_Piece like ('____F0')OR DO_Piece like ('____G0')
OR DO_Piece like ('____H0')OR DO_Piece like ('____I0')OR DO_Piece like ('____J0')OR DO_Piece like ('____K0')OR DO_Piece like ('____L0')OR DO_Piece like ('____M0')
OR DO_Piece like ('____N0')OR DO_Piece like ('____O0')OR DO_Piece like ('____P0')OR DO_Piece like ('____Q0')OR DO_Piece like ('____R0')OR DO_Piece like ('____S0')
OR DO_Piece like ('____T0')OR DO_Piece like ('____U0')OR DO_Piece like ('____V0')OR DO_Piece like ('____W0')OR DO_Piece like ('____X0')OR DO_Piece like ('____Y0')
OR DO_Piece like ('____Z0')OR DO_Piece like ('____D0'))and DO_Date between ('01/01/2018') and('31/12/2018') and do_type in ('16','17')
open cur1
fetch cur1 into @num_p 
while @@FETCH_STATUS=0
begin
select  f_docligne.do_piece,f_docligne.ct_num,F_COMPTET.CT_Intitule,AR_Ref, DL_Design,DL_Qte, DL_PrixUnitaire,DL_PUDevise,DL_Frais,DL_PUTTC, DL_MontantHT,
 DL_MontantTTC,DO_Ventile,DO_Cours,f_docligne.do_date from F_DOCLIGNE,F_DOCENTETE,F_COMPTET
 WHERE  f_docligne.DO_Piece=F_DOCENTETE.DO_Piece and F_DOCLIGNE.DO_Type=F_DOCENTETE.DO_Type and f_docligne.CT_Num=F_DOCENTETE.DO_Tiers 
 and F_DOCENTETE.DO_Tiers=F_COMPTET.CT_Num and f_docligne.DO_Piece like((select LEFT(@num_p,5))+'_') order by f_docligne.DO_Piece
fetch cur1 into @num_p 
end
close cur1
deallocate cur1

问题是在sqlserver中,我在提取时看到所有记录 但是在delphi应用程序中,它仅返回最后一次获取,因此,如何像SQLSERVER那样获取它,并按显示的顺序显示数据

2 个答案:

答案 0 :(得分:2)

这不能像SQL Server一样回答如何获得查询的问题,但是您可以通过重写查询来检索结果,因此不需要游标。
在对模式知之甚少的情况下,很难更改查询,但是我还是尝试了。
您确实应该使用较新的联接样式,它已有20多年的历史了,并且可读性更高。
还可以使用与区域无关的日期格式,例如'yyyyMMdd'。

这应该可以帮助您入门,但是您需要填写很多???偏离路线

select  L.do_piece, L.ct_num, E.CT_Intitule, ???.AR_Ref, ???.DL_Design, ???.DL_Qte, ???.DL_PrixUnitaire, ???.DL_PUDevise, 
        ???.DL_Frais, ???.DL_PUTTC, ???DL_MontantHT, ???.DL_MontantTTC, ???.DO_Ventile, ???.DO_Cours, L.do_date 
from    F_DOCLIGNE L
  inner join F_DOCENTETE E on L.DO_Piece = E.DO_Piece
                          and L.DO_Type = E.DO_Type
                          and L.CT_Num = E.DO_Tiers
  inner join F_COMPTET P on E.DO_Tiers = P.CT_Num

where (E.DO_Piece like ('____00') or E.DO_Piece like ('____A0') or E.DO_Piece like ('____B0') 
       or E.DO_Piece like ('____B0') or E.DO_Piece like ('____C0') OR E.DO_Piece     like ('____D0') OR E.DO_Piece like ('____E0') OR E.DO_Piece like ('____F0') OR     E.DO_Piece like ('____G0')
       OR E.DO_Piece like ('____H0') OR E.DO_Piece like ('____I0') OR E.DO_Piece like ('____J0') OR E.DO_Piece like ('____K0') OR E.DO_Piece like ('____L0') OR E.DO_Piece like ('____M0')
       OR E.DO_Piece like ('____N0') OR E.DO_Piece like ('____O0') OR E.DO_Piece like ('____P0') OR E.DO_Piece like ('____Q0') OR E.DO_Piece like ('____R0') OR E.DO_Piece like ('____S0')
       OR E.DO_Piece like ('____T0') OR E.DO_Piece like ('____U0') OR E.DO_Piece like ('____V0') OR E.DO_Piece like ('____W0') OR E.DO_Piece like ('____X0') OR E.DO_Piece like ('____Y0')
       OR E.DO_Piece like ('____Z0') OR E.DO_Piece like ('____D0')
      )
and   E.DO_Date between ('20180101') and('20181231') 
and   E.do_type in ('16','17')

order by L.DO_Piece

或者按照@IvanStarostin的建议,您可以

select  L.do_piece, L.ct_num, E.CT_Intitule, ???.AR_Ref, ???.DL_Design, ???.DL_Qte, ???.DL_PrixUnitaire, ???.DL_PUDevise, 
        ???.DL_Frais, ???.DL_PUTTC, ???DL_MontantHT, ???.DL_MontantTTC, ???.DO_Ventile, ???.DO_Cours, L.do_date 
from    F_DOCLIGNE L
  inner join F_DOCENTETE E on L.DO_Piece = E.DO_Piece
                          and L.DO_Type = E.DO_Type
                          and L.CT_Num = E.DO_Tiers
  inner join F_COMPTET P on E.DO_Tiers = P.CT_Num

where E.DO_Piece like '___[0A-Z]0'
and   E.DO_Date between ('20180101') and('20181231') 
and   E.do_type in ('16','17')

order by L.DO_Piece

答案 1 :(得分:2)

您的SQL语句返回多个结果集,因此您需要获取每个结果集。 我在旧的Delphi7应用程序中使用了下一种方法,它为我工作。您只需要调用NextRecordset()方法即可。

Delphi部分:

procedure TForm1.btnOpenClick(Sender: TObject);
var
   s: string;
   rs: _RecordSet;
   n: Integer;
begin
   // Here, I assume that you have TADOQuery component already created,
   // with correct Connection or ConnectionString property.
   s := 'Your SQL statement';
   try
      if qry.Active then qry.Close;
      qry.SQL.Clear;
      qry.SQL.Add(s);
      qry.Open;
   except
      on E: Exception do begin
         ShowMessage('Error ' + E.Message);
         Exit;
      end{on};
   end{try};

   // Consume multiple resultsets
   rs := qry.Recordset;
   repeat
      while not rs.Eof do begin
         // Do what you want with fields values.
         // ShowMessage(rs.Fields['FieldName'].Value);
         rs.MoveNext;
      end{while};
      rs := qry.NextRecordset(n);
   until (rs = nil);

   //
   qry.Close;
   ShowMessage('OK');
end;