Create a function for counting rows from any table

soulblazer

I have this user-defined function:

 CREATE FUNCTION [dbo].[COUNT_ROWS_TABLE]()
 RETURNS TINYINT
 AS
 BEGIN
      DECLARE @ROW_COUNT TINYINT
      SELECT @ROW_COUNT = COUNT(*) FROM EMPLOYEE
      RETURN @ROW_COUNT
 END
 GO

The problem is it only works for the table [dbo].[EMPLOYEE] and I don't want to Copy-Paste this function for every table on my database.

My attempt so far:

 CREATE FUNCTION [dbo].[COUNT_ROWS_TABLE](@TABLE_NAME VARCHAR(50))
 RETURNS TINYINT
 AS
 BEGIN
      DECLARE @SQL_COMMAND NVARCHAR(100)
      DECLARE @PARAM NVARCHAR(50)
      DECLARE @ROW_COUNT TINYINT
      SET @SQL_COMMAND = N'SELECT @RESULT = COUNT(*) FROM ' + @TABLE_NAME
      SET @PARAM = N'@RESULT TINYINT OUTPUT'
      EXEC SP_EXECUTESQL @SQL_COMMAND, @PARAM, @RESULT = @ROW_COUNT OUTPUT
      RETURN @ROW_COUNT
 END
 GO

That code does not work because it doesn't allow those statements inside a function. It works inside a stored procedure, though, but only if I PRINT the variable rather than RETURN it.

I need it to be a function, since I need to call it on an IF statement.

Any thoughts on how to achieve this? Thank you.

Pragnesh Khalas

You can use like below

ALTER FUNCTION [dbo].[COUNT_ROWS_TABLE](@TABLE_NAME VARCHAR(50))
 RETURNS TINYINT
AS
BEGIN
DECLARE @ROW_COUNT TINYINT
SELECT @ROW_COUNT = SUM(b.rows) 
FROM SYS.TABLES a INNER JOIN sys.partitions b ON b.OBJECT_ID = a.OBJECT_ID
WHERE a.is_ms_shipped = 0 AND b.index_id IN (1,0)
        and a.name=@TABLE_NAME
GROUP BY a.name

RETURN @ROW_COUNT
END
GO

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Counting rows from table

From Dev

Count function not counting all rows in db table

From Dev

COUNTING mysql table rows based on 2 conditions from another table

From Dev

Simple javascript counting table rows

From Dev

Counting related rows in a child table

From Dev

Subsetting Data Table / Counting rows

From Dev

Javascript counting adaptive table rows

From Dev

counting no of specific rows in a table using jquery(.each() function) also using .attr() function

From Dev

Create multiple rows in table from array?

From Dev

Create table from two rows, one column

From Dev

SQL Create table from surrounding rows

From Dev

Counting rows without any NaNs in a struct

From Dev

Counting/totaling rows in a create view statement, mysql

From Dev

Counting rows in data.table that meet a condition

From Dev

VBA: Counting rows in a table (list object)

From Dev

Counting previous rows in a data table based on date

From Dev

PHP PDO counting all table rows in Database

From Dev

Counting certain values in rows in SAS table

From Dev

PHP PDO counting all table rows in Database

From Dev

Counting rows in Vertica table per JDBC

From Dev

Counting occurrence of unique rows and produce summary in table

From Dev

Selecting data from multiple rows from a temporary table create by SELECT

From Dev

plpgsql function: Return rows from a view created from random table

From Dev

Create table with all pairs of values from one column in R, counting unique values

From Dev

Updating a Table by counting results from another table

From Dev

Counting rows from different tables in same query

From Dev

Counting rows from a GROUP BY query using an index

From Dev

counting rows from a cursor in pl/sql

From Dev

Counting the number of rows from db with ajax

Related Related

  1. 1

    Counting rows from table

  2. 2

    Count function not counting all rows in db table

  3. 3

    COUNTING mysql table rows based on 2 conditions from another table

  4. 4

    Simple javascript counting table rows

  5. 5

    Counting related rows in a child table

  6. 6

    Subsetting Data Table / Counting rows

  7. 7

    Javascript counting adaptive table rows

  8. 8

    counting no of specific rows in a table using jquery(.each() function) also using .attr() function

  9. 9

    Create multiple rows in table from array?

  10. 10

    Create table from two rows, one column

  11. 11

    SQL Create table from surrounding rows

  12. 12

    Counting rows without any NaNs in a struct

  13. 13

    Counting/totaling rows in a create view statement, mysql

  14. 14

    Counting rows in data.table that meet a condition

  15. 15

    VBA: Counting rows in a table (list object)

  16. 16

    Counting previous rows in a data table based on date

  17. 17

    PHP PDO counting all table rows in Database

  18. 18

    Counting certain values in rows in SAS table

  19. 19

    PHP PDO counting all table rows in Database

  20. 20

    Counting rows in Vertica table per JDBC

  21. 21

    Counting occurrence of unique rows and produce summary in table

  22. 22

    Selecting data from multiple rows from a temporary table create by SELECT

  23. 23

    plpgsql function: Return rows from a view created from random table

  24. 24

    Create table with all pairs of values from one column in R, counting unique values

  25. 25

    Updating a Table by counting results from another table

  26. 26

    Counting rows from different tables in same query

  27. 27

    Counting rows from a GROUP BY query using an index

  28. 28

    counting rows from a cursor in pl/sql

  29. 29

    Counting the number of rows from db with ajax

HotTag

Archive