SQL Clone
SQLServerCentral is supported by Redgate
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:


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: 2720 | Views in the last 30 days: 1
Related Articles

Format Phone Number function

Function to format phone numbers entered as text.


Alternate of Row_Number() in SQL 2000

Row_Number() function in SQL2000.


Function to get number of days in month

Function to get number of days in month


Function to format a number

My number formatting function doesn't work as expected


True WHOLE NUMBER function.

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