Board index » delphi » Grabbing Variable Number of Records from TSQL

Grabbing Variable Number of Records from TSQL

Please help. I have a routine that executes a SQL statement and returns a
variable number of records (each record has two fields). I need to grab these
records and write them to one line in a text file. I am lost as to how to
reference all of the records in a TDataset for writing to a single line. Any
ideas?

Example: Record1=Field1, Field2
         Record2=Field1, Field2
         Record3=Field1, Field2
Need to write to a file so that I have a text line that looks like this
(R1F1,R1F2,R2F1,R2F2,R3F1,R3F2). It is easy to script this if I always have the
same number of records in the result set, but I could have from 1 to 30 records
each time this SQL statement runs in its loop!

 

Re:Grabbing Variable Number of Records from TSQL


Matthew,

Try something like this:
        query.open;  // returns some number of records
        query.first;
        str := '';  // clear a string variable
        while not query.EOF do
                str := str + field1.value + field2.value;
                next;
        // after looping through the result set, str will be one
string with all the field values added in order.

HTH,
Dan

On 9 Jul 1998 00:27:44 GMT, m_stan...@tetco.com (Matthew Stanton)
wrote:

Quote
>Please help. I have a routine that executes a SQL statement and returns a
>variable number of records (each record has two fields). I need to grab these
>records and write them to one line in a text file. I am lost as to how to
>reference all of the records in a TDataset for writing to a single line. Any
>ideas?

>Example: Record1=Field1, Field2
>     Record2=Field1, Field2
>     Record3=Field1, Field2
>Need to write to a file so that I have a text line that looks like this
>(R1F1,R1F2,R2F1,R2F2,R3F1,R3F2). It is easy to script this if I always have the
>same number of records in the result set, but I could have from 1 to 30 records
>each time this SQL statement runs in its loop!

--
Dan Brennand
CMDC systems, inc.
Configuration Management and Document Control: Consulting, Software,
and the only comprehesive textbook on this subject.
visit us at www.cmdcsystems.com
[Remove the SPAM from my e-mail address]

Other Threads