Search
Web Braintrove
Site Navigation
Home
Products
Tags
Levels
Dates
Authors
5 MOST RECENT
Conditionally Display Different Values In a Single Expression Box
Create a Button With a Blank Label
Create Shared Rules
Perform a Case-Insensitive String Comparison without Using Translate()
Force a Field Value to a Boolean Using Rules
5 MOST POPULAR
Pass Query String Parameters to an ASP.NET Xml Control
Access a Method in a Master Page with Code-Behind
Prevent Namespace Prefixes from Being Copied to the Output
Create Groups in a Drop-Down List Box
Change the Default Action of the Power Button on the Windows Vista Start Menu
5 MOST FORGOTTEN
Hide Deleted Items in the Source Control Explorer
Recover a Dark Picture with a Heavy Color Cast
Prevent Windows Vista from Requesting Your Permission to Continue
Create a Variable with a Unique Sorted Node-Set
Prevent Users from Seeing ASP.NET Error Pages
Microsoft MVP
This article has been viewed 276 times.

Select Table Row Counts as Columns

Written by Gregory Scot Collins
Monday, 31 December 2007, 8:49 AM
This article has been tested to work with the following products and versions. No guarantee of compatibility, with or without modification, is offered for products or versions other than those listed.
If there is a need to identify the number of rows in one or more tables of your SQL database, you can easily get these counts and return them as columns of a SELECT clause. In this article, we will show the basic syntax required for achieving this, and then show an actual example using the AdventureWorks sample database.

Syntax for selecting table row counts as columns

The method of selecting table row counts as columns is simple and straighforward. Listing 1 shows the syntax as performing selects within your main SELECT clause. These inner-SELECT clauses must be contained within parentheses. The COUNT(*) function returns the number of rows in the specified table. The highlighted portions of Listing 1 identify places where you need to substitute in your actual table names and your desired resulting column names.

Selecting row counts using the AdventureWorks sample database

Now we will apply the syntax to an actual SELECT clause using the AdventureWorks sample database that ships with Microsoft SQL Server 2005. If you do not have this sample database installed, you can install it following the instructions contained within the SQL Server online help. Listing 2 gets the counts of four different tables and names the columns appropriately.
The result of the SELECT clause in Listing 2 is shown in Screenshot 1. Any number of table row counts can be returned in this manner.
Bookmark this Article
StumbleUpon  Stumble It!
Digg  Digg It!
del.icio.us  del.icio.us
List of Figures
Listing 1 - Selecting row counts as columns.
Listing 2 - Selecting row counts as columns from the AdventureWorks sample database.
Screenshot 1 - Results of the AdventureWorks row count selection.
Article Tags
Great Deals
TigerDirect Exclusive Deals, Limited Time Offers, Act Now And Save!
Find all current special offers on Adobe products.
Try SugarSync Free!
Join WebHost4Life.com
TigerDirect
Computers4SURE (4SURE.com - An Office Depot Co.)
Copyright © 2006-2010 Braintrove. All rights reserved. Braintrove, braintrove.com, and the Braintrove logo are trademarks of Gregory Scot Collins in the United States and/or other countries. The names of actual companies and products mentioned herein may be the trademarks of their respective owners. Any rights not expressly granted herein are reserved.