Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

Generic COUNT(*)

By stevanus wibowo,

Counting the number of records in a table is a common task in every application. It could be the number of users, orders, etc. The query itself in the simplest form would be:

SELECT COUNT(*) FROM [table];

While we could launch the query from the application's data access layer, it would normally be nice to have SQL codes in database in the form of stored procedures or functions. However, creating such module for each table would just grow its number linearly as the number of tables. With dynamic SQL, we could write one module that could be applied to all tables.

Though it might be useful only for a small subset of scenarios (since most scenarios require record filtering); it has dramatically reduced the number of functions you need to create by a factor of n; where n is the number of tables.

Note: ideally it would be a function but since we can't launch dynamic SQL inside it then stored procedure would just perform fine.

Total article views: 2649 | Views in the last 30 days: 5
 
Related Articles
SCRIPT

Format Phone Number function

Function to format phone numbers entered as text.

FORUM

Alternate of Row_Number() in SQL 2000

Row_Number() function in SQL2000.

SCRIPT

Function to get number of days in month

Function to get number of days in month

FORUM

Function to format a number

My number formatting function doesn't work as expected

FORUM

True WHOLE NUMBER function.

Need to create True WHOLE NUMBER function that does not contains -+?/ and all operators

Tags
t-sql    
 
Contribute

Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones
Editor, SQLServerCentral.com

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones