Manipulation of sql temporary tables in delphi

1

Good afternoon I create in Delphi a temporary Sql table, that is, it does not exist physically. I can even write data to it, but I can not put the information in a dbgrid:

1) I created the table like this:

qryCIDtemp.SQL.add('Create table ''#tempCiD''(SUS varchar(50), Saude varchar(50), Diferenca varchar(30), dtCompetencia char(6)');  //Cria tabela temporária
qryCIDtemp.Close;
qryCIDtemp.sql.clear;// limpa conteúdo do dataset

qryCIDtemp.sql.Add('Insert into ''#tempcid''(SUS,Sanitas,Diferenca,dtCompetencia)');
qryCIDtemp.sql.Add('values (:SUS,:Saude,:Diferenca,:dtCompetencia)');

2) I recorded inside while not eof ..., just a part of the code here for you to understand:

qryCiDtemp.ParamByname('SuS').value    := qryCidProcedimentoBKP.FieldByName('cdcid10').AsString ;
qryCIDtemp.ParamByname('Saude').value:= qryCid.FieldByName('cdcid10').AsString ;
qryCIDtemp.ParamByname('Diferenca').value:= 'CID';

3) I do not know if it was necessary but I used but the following command:

qryCIDtemp.SQL.add('Select * from ''#tempCID''');

4) Only after this I tried to show the result in dbgrid using the code: obs: dbgrid is without datasource

with qryCIDtemp do
       begin
            dbgrid2.Columns.Clear;
            dbgrid2.Columns.Add;
            dbgrid2.Columns[0].FieldName :='sus';
            dbgrid2.Columns[0].Title.Caption := 'Informação SUS';
            dbgrid2.Columns[0].Width := 500;
            dbgrid2.Columns.Add;
            dbgrid2.Columns[1].FieldName := 'SAuDE';
            dbgrid2.Columns[1].Title.Caption := 'Informação Saúde';
            dbgrid2.Columns[1].Width := 500;
            dbgrid2.Columns.Add;
            dbgrid2.Columns[2].FieldName := 'Diferença';
            dbgrid2.Columns[2].Title.Caption := 'Diferença';
            dbgrid2.Columns[2].Width := 200;
       end;

dbgrid even shows the caption's filled and width correctly, but it is empty.

In the form I have a query named qryCIDtemp, in the string property there is no SQL statement. Has a DaSource connected to qryCIDtemp

The DataSource property of qryCIDtemp is empty, do I have to put some information here? How temporary I do not know what to fill.

If someone can help me, I'm very grateful.

    
asked by anonymous 21.06.2018 / 20:08

1 answer

0

Delphi querys only make direct connection to the database they do not store temporary data.

To do what you want to use TClientDataSet (Delphi 7) or TFDMemTable (Delphi XE5 +)

In case you will not deal with creates and inserts, you would do +/- so

oMemTable.Append;
oMemTable.FieldByName('Saude').value     := qryCid.FieldByName('cdcid10').AsString ;
oMemTable.FieldByName('Diferenca').value := 'CID';
oMemTable.Post;

Then, when you want to save to the bank, you loop the oMemTable and save each record.

    
21.06.2018 / 20:35