-- ================================================
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Ayman A. Nour
-- Create date: 14/10/2009
-- Description:
-- =============================================
CREATE PROCEDURE sp_DB_Tables_Info
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
DECLARE @Table_Type nvarchar(10), @Table_Name nvarchar(255), @Row_Count integer
DECLARE @ResultTable TABLE(Table_Type nvarchar(10), Table_Name nvarchar(255), Rows_Count integer)
DECLARE TABLES_LIST CURSOR FOR
SELECT DISTINCT
o.type
, o.name
FROM sys.objects o LEFT OUTER JOIN sys.partitions p ON p.object_id = o.object_id
WHERE o.type IN ( 'U', 'V')
ORDER BY o.type, o.name
DECLARE @tmpSQL nvarchar(1000)
OPEN TABLES_LIST
FETCH FROM TABLES_LIST INTO @Table_Type, @Table_Name
WHILE(@@FETCH_STATUS <> -1)
BEGIN
SET @tmpSQL = N'SELECT @Row_Count = COUNT(*) FROM ' + @Table_Name
BEGIN TRY
EXEC sp_executesql @tmpSQL, N'@Row_Count integer output', @Row_Count=@Row_Count output
END TRY
BEGIN CATCH
PRINT 'An Error occured during counting rows of ' + @Table_Name
END CATCH
INSERT INTO @ResultTable VALUES(@Table_Type, @Table_Name, @Row_Count)
FETCH NEXT FROM TABLES_LIST INTO @Table_Type, @Table_Name
END
CLOSE TABLES_LIST
DEALLOCATE TABLES_LIST
SELECT * FROM @ResultTable
END
GO
-- To execute the stored
EXEC sp_DB_Tables_Info
|