Home > Ask the Oracle Experts > SQL Questions & Answers > Result set row count along with query results
Ask The Oracle Expert: Questions & Answers
EMAIL THIS

Result set row count along with query results

Rudy Limeback EXPERT RESPONSE FROM: Rudy Limeback

Pose a Question
Other Oracle Categories
Meet all Oracle Experts
Become an Expert for this site


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


>
QUESTION POSED ON: 16 July 2002
Can I get both the result of the query and the number of rows returned in a single query?

>
EXPERT RESPONSE

Every database has a facility to return a status code to indicate whether a query was successful. Along with this, the database also usually returns the number of rows processed. For example, if you run a SELECT query in Oracle, it will say "nn rows selected." Microsoft's SQL/Server will say "nn rows affected" (even though the rows aren't really "affected" by a SELECT).

If you are using an application with scripting capabilities, like a Web interface developed with ASP or PHP or ColdFusion, for example, then this row count is also available for every query you run.

<cfquery name="getrecs" datasource="#yourdb#">

  select foo, bar
    from yourtable
   where baz = "quux"
</cfquery>

<cfoutput> <p> The number of records is: #getrecs.RecordCount# </p> </cfoutput>

You can also retrieve the number of records via SQL, although it's a bit of a hack, because in order to do it in one query, you have stretch the definition of "one query" to cover a UNION with two subqueries.

( select 1 as rowtype
       , foo
       , bar
    from yourtable  
   where baz = "quux" )
union all
( select 2 
       , count(*)
       , sum(bar)
    from yourtable
   where baz = "quux" )
order by rowtype, foo

A good optimizer should be able to detect the same WHERE clause and not make two complete passes of the table. Note that the rowtype column is used both to distinguish "detail" rows from the "total" row, and also as the major sort field, so that the total row comes last. The count of rows is returned in the foo column, and just for fun, the sum of the bar column values is returned in the bar column.

So this "hack" is actually a good hack, in that it can be used for other aggregates like SUM() for which no standard mechanism (like the number of rows processed) is provided.

For More Information


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


RELATED CONTENT
SQL
IN list or series of OR conditions?
Connecting tables in a database
SQL query for co-authored books
Querying complex derived tables
SQL string functions
Changing a NULL column to NOT NULL
SQL for hourly totals for the last 48 hours
LEFT OUTER JOIN to a MIN/MAX row
Normalizing a crosstab table
Querying metadata and data at the same time

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary



Search and Browse the Expert Answer Center
Search and browse more than 25,000 question and answer pairs from more than 250 TechTarget industry experts.
Browse our Expert Advice

HomeNewsTopicsTipsAsk the ExpertsMultimediaWhite PapersProductsBlogs
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides enterprise IT professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective IT purchase decisions and managing their organizations' IT projects - with its network of technology-specific Web sites, events and magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Reprints  |  Site Map




All Rights Reserved, Copyright 2003 - 2008, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts