Board index » delphi » Is there a way to run a query against the result set another query

Is there a way to run a query against the result set another query

Greetings All,

Currently I have to query my database twice.  One for the actual result set
and another returning the count of the result set.  Both queries have the
exact same SQL statement with the exception of the count query which uses
something like:

SQL.Add('Select count(*) from Employer E ' +
                             'LEFT OUTER JOIN Emp_Address EA ON EA.Emp_ID =
E.Emp_ID '+
                             'and EA.Addr_Type_Code = "P" ' +
                             'and EA.Default_Address = 1 ' + SQLText);

Is there anyway to run a query that counts records against the first query
that returns the actual result set?

Seems to me would be much faster than actually querying the same table
twice.

Or better yet, why doesn't the RecordCount property of an IBQuery return the
record count?

Any help would be appreciated.

Thanks

Mike

 

Re:Is there a way to run a query against the result set another query


Not really.  With the BDE you can make a permanent table out of the result set,
but in this case there isn't really an easy way to do this.

As for record count, most SQL backends don't return record counts.  With IBX,
RecordCount will reflect the number of records brought across to the client
side, so in some situations where the query is long running, doing a FetchAll
will fetch all the records across and RecordCount will then reflect the real
count.

Quote
M Tuttle wrote:

> Greetings All,

> Currently I have to query my database twice.  One for the actual result set
> and another returning the count of the result set.  Both queries have the
> exact same SQL statement with the exception of the count query which uses
> something like:

> SQL.Add('Select count(*) from Employer E ' +
>                              'LEFT OUTER JOIN Emp_Address EA ON EA.Emp_ID =
> E.Emp_ID '+
>                              'and EA.Addr_Type_Code = "P" ' +
>                              'and EA.Default_Address = 1 ' + SQLText);

> Is there anyway to run a query that counts records against the first query
> that returns the actual result set?

> Seems to me would be much faster than actually querying the same table
> twice.

> Or better yet, why doesn't the RecordCount property of an IBQuery return the
> record count?

> Any help would be appreciated.

> Thanks

> Mike

--
Jeff Overcash (TeamB)
      (Please do not email me directly unless  asked. Thank You)
Maybe it was infatuation or the thrill of the chase.
Maybe you were always beyond my reach and my heart was playing safe.
But was that love in your eye I saw or the reflection of mine?
Give me time, won't you give me that time!
Welcome back to the circus.            (Fish)

Re:Is there a way to run a query against the result set another query


Thanks Jeff,

Will there be a penalty using fetchAll for:
1000 records
5000 records
10000 records
etc?

Thanks

Mike

"Jeff Overcash (TeamB)" <overc...@onramp.net> wrote in message
news:38F35DED.173A7CAD@onramp.net...

Quote
> Not really.  With the BDE you can make a permanent table out of the result
set,
> but in this case there isn't really an easy way to do this.

> As for record count, most SQL backends don't return record counts.  With
IBX,
> RecordCount will reflect the number of records brought across to the
client
> side, so in some situations where the query is long running, doing a
FetchAll
> will fetch all the records across and RecordCount will then reflect the
real
> count.

> M Tuttle wrote:

> > Greetings All,

> > Currently I have to query my database twice.  One for the actual result
set
> > and another returning the count of the result set.  Both queries have
the
> > exact same SQL statement with the exception of the count query which
uses
> > something like:

> > SQL.Add('Select count(*) from Employer E ' +
> >                              'LEFT OUTER JOIN Emp_Address EA ON
EA.Emp_ID =
> > E.Emp_ID '+
> >                              'and EA.Addr_Type_Code = "P" ' +
> >                              'and EA.Default_Address = 1 ' + SQLText);

> > Is there anyway to run a query that counts records against the first
query
> > that returns the actual result set?

> > Seems to me would be much faster than actually querying the same table
> > twice.

> > Or better yet, why doesn't the RecordCount property of an IBQuery return
the
> > record count?

> > Any help would be appreciated.

> > Thanks

> > Mike

> --
> Jeff Overcash (TeamB)
>       (Please do not email me directly unless  asked. Thank You)
> Maybe it was infatuation or the thrill of the chase.
> Maybe you were always beyond my reach and my heart was playing safe.
> But was that love in your eye I saw or the reflection of mine?
> Give me time, won't you give me that time!
> Welcome back to the circus.            (Fish)

Re:Is there a way to run a query against the result set another query


An alterantive I use if I will be bringing the whole data into the client:
put all the data into TClientDataSet. You get the record count, plus all the
extra goodies (creating indexes on the dataset, using aggregates, persisting
to and from disk, etc).

Marcelo Lopez Ruiz

Quote
M Tuttle <mi...@softtechks.com> wrote in message news:38f353cc@dnews...
> Greetings All,

> Currently I have to query my database twice.  One for the actual result
set
> and another returning the count of the result set.  Both queries have the
> exact same SQL statement with the exception of the count query which uses
> something like:

> SQL.Add('Select count(*) from Employer E ' +
>                              'LEFT OUTER JOIN Emp_Address EA ON EA.Emp_ID
=
> E.Emp_ID '+
>                              'and EA.Addr_Type_Code = "P" ' +
>                              'and EA.Default_Address = 1 ' + SQLText);

> Is there anyway to run a query that counts records against the first query
> that returns the actual result set?

> Seems to me would be much faster than actually querying the same table
> twice.

> Or better yet, why doesn't the RecordCount property of an IBQuery return
the
> record count?

> Any help would be appreciated.

> Thanks

> Mike

Re:Is there a way to run a query against the result set another query


Only a network penalty of pulling all the data local.  Over dialup lines this
could be costly, over normal networks much less so.

Marcelo's suggestion of TClientDataset is also a good one, but has the exact
same performance penalty of requiring all the data to be brought local.

Quote
M Tuttle wrote:

> Thanks Jeff,

> Will there be a penalty using fetchAll for:
> 1000 records
> 5000 records
> 10000 records
> etc?

> Thanks

> Mike

--
Jeff Overcash (TeamB)
      (Please do not email me directly unless  asked. Thank You)
Maybe it was infatuation or the thrill of the chase.
Maybe you were always beyond my reach and my heart was playing safe.
But was that love in your eye I saw or the reflection of mine?
Give me time, won't you give me that time!
Welcome back to the circus.            (Fish)

Re:Is there a way to run a query against the result set another query


Thanks Marcelo and All who replied,

Well if I would have given the full picture, I would have stated that I am
using CDS in a two tier enviroment.  DataSource -> ClientDataset ->
DatasetProvider -> IBQuery

For some reason I never even considered the RecordCount of the CDS.  This is
exactly what I needed.

Thanks again for the pointer.

Mike
"Marcelo Lopez Ruiz" <marcelo.lopezr...@xlnet.com.ar> wrote in message
news:8cvrl2$sfg4@bornews.borland.com...

Quote
> An alterantive I use if I will be bringing the whole data into the client:
> put all the data into TClientDataSet. You get the record count, plus all
the
> extra goodies (creating indexes on the dataset, using aggregates,
persisting
> to and from disk, etc).

> Marcelo Lopez Ruiz

> M Tuttle <mi...@softtechks.com> wrote in message news:38f353cc@dnews...
> > Greetings All,

> > Currently I have to query my database twice.  One for the actual result
> set
> > and another returning the count of the result set.  Both queries have
the
> > exact same SQL statement with the exception of the count query which
uses
> > something like:

> > SQL.Add('Select count(*) from Employer E ' +
> >                              'LEFT OUTER JOIN Emp_Address EA ON
EA.Emp_ID
> =
> > E.Emp_ID '+
> >                              'and EA.Addr_Type_Code = "P" ' +
> >                              'and EA.Default_Address = 1 ' + SQLText);

> > Is there anyway to run a query that counts records against the first
query
> > that returns the actual result set?

> > Seems to me would be much faster than actually querying the same table
> > twice.

> > Or better yet, why doesn't the RecordCount property of an IBQuery return
> the
> > record count?

> > Any help would be appreciated.

> > Thanks

> > Mike

Other Threads