How to include data from a multi-line query in a memo using Delphi?

Asked

Viewed 644 times

0

Good afternoon.

Work on a legacy system and need to put query information into a single memo object.

Code follows.

MyQuery.Close;

with MyQuery.SQL do
begin
     clear;
     add(' select TOP 8 nuentrada, dstpleitu, vlleitura, dshint, dtleitura, hhleitura ');
     add(' from ssdentsinaisvitais inner join ssttplei on ssdentsinaisvitais.cdleitura = ssttplei.cdleitura ');
     add(' where nuentrada = ' + lblnuentrada.Caption);
     add(' order by dtleitura desc, hhleitura desc ');
 end;

 MyQuery.Open;

 while MyQuery.Eof do
 begin
      mmdsexamefisico.Text := mmdsexamefisico.Text + MyQuery.DataSource.DataSet.FieldByName('dstpleitu').AsString + ': ';
      mmdsexamefisico.Text := mmdsexamefisico.Text + MyQuery.DataSource.DataSet.FieldByName('vlleitura').AsString + #13#10;
 end; 

A syntax error occurs next to the word order.

Can detect the error?


To query formed is the following:

SELECT TOP 8 nuentrada,
             dstpleitu,
             vlleitura,
             dshint,
             dtleitura,
             hhleitura
  FROM ssdentsinaisvitais
       INNER JOIN ssttplei ON ssdentsinaisvitais.cdleitura = ssttplei.cdleitura 
 WHERE nuentrada = 350000701778
 ORDER BY dtleitura DESC,
       hhleitura DESC
  • 1

    Before the Open give a ShowMessage(MyQuery.SQL.Text); and inform what the query that was formed;

  • Hello! select TOP 8 nuentrada, dstpleitu, vlleitura, dshint, dtleitura, hhleitura 
from ssdentsinaisvitais inner join ssttplei on ssdentsinaisvitais.cdleitura = ssttplei.cdleitura 
where nuentrada = 350000701778
order by dtleitura desc, hhleitura desc

  • 1

    It is explained then, the maximum size of the integer in SQL Server is 2147483647

  • You have to see then if you do not want to pass as string, if you want: add(' where nuentrada = ''' + lblnuentrada.Caption + '''');

  • Glue the mistake for us there

  • 2

    Did you try to pass another value instead of 350000701778? for example 100.

    1. What is the data type of the "new" column? 2) You are using Tfdquery, Tadoquery .. etc?
  • Hello, everyone! I am using the Tquery object.

Show 3 more comments

2 answers

0


Good afternoon to all.

The problem was solved by placing a Myquery.next at the end of while, as below.

MyQuery.Close;

with MyQuery.SQL do
begin
     clear;
     add(' select TOP 8 nuentrada, dstpleitu, vlleitura, dshint, dtleitura, hhleitura ');
     add(' from ssdentsinaisvitais inner join ssttplei on ssdentsinaisvitais.cdleitura = ssttplei.cdleitura ');
     add(' where nuentrada = ' + lblnuentrada.Caption);
     add(' order by dtleitura desc, hhleitura desc ');
 end;

 MyQuery.Open;

 while MyQuery.Eof do
 begin
      mmdsexamefisico.Text := mmdsexamefisico.Text + MyQuery.DataSource.DataSet.FieldByName('dstpleitu').AsString + ': ';
      mmdsexamefisico.Text := mmdsexamefisico.Text + MyQuery.DataSource.DataSet.FieldByName('vlleitura').AsString + #13#10;
      MyQuery.next;
 end; 

0

You are using the data type BIGINT instead of the data type INT, as the maximum value of INT is 2.147.483.647, you can consult the on-line documents[1] here to see. Also consider using parameters to avoid these errors and injection of SQL [2].

If you are using TFDQuery:

Const
  Num: Largeint = 350000701778;
Var
  FDQuery: TFDQuery;
begin
  FDQuery := TFDQuery.Create(Nil);
  try
    with FDQuery do
      begin
        Connection:= FDConnection1;
        SQL.Text:= 'SELECT ... WHERE Table.nuentrada = :Param';
        Params.CreateParam(ftLargeint, 'Param', ptInput);
        ParamByName('Param').AsLargeInt:= Num;
        Open();
        DataSource.DataSet:= FDQuery;
      end;
  finally
    //FD.Free;
  end;
end;

If you are using TADOQuery:

Const
  Num: Largeint = 350000701778;
Var
  ADOQuery: TADOQuery;
begin
  ADOQuery := TADOQuery.Create(Nil);
  try
    with ADOQuery do
      begin
        Connection:= ADOConnection1;
        SQL.Text:= 'SELECT ... WHERE Table.nuentrada = :Param';
        Parameters.ParamByName('Param').Value:= Num;
        Open;
        DataSource.DataSet:= ADOQuery;
      end;
  finally
    //ADOQuery.Free;
  end;
end;

[1] on-line documents.

[2] Injection of SQL.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.