Monday, March 24, 2008

Rowcounts in PS tables

Here is the SQL that you can use to fetch the number of rows in the PS tables in MS SQL Server database.

SET NOCOUNT ON
DECLARE @tablename VARCHAR (128)
-- Declare cursor
DECLARE tables CURSOR FOR
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_SCHEMA = 'dbo'
AND TABLE_NAME LIKE 'PS_%'
-- Create the table
CREATE TABLE #tables (
tablet CHAR (255),
RecordCount INT)
-- Open the cursor
OPEN tables
-- Loop through all the tables in the database
FETCH NEXT
FROM tables
INTO @tablename

WHILE @@FETCH_STATUS = 0
BEGIN
-- Do the showcontig of all indexes of the table
INSERT INTO #tables
exec('SELECT '+''''+ @tablename + ''''+ ', COUNT(*) FROM '+ @tablename)

FETCH NEXT
FROM tables
INTO @tablename
END

-- Close and deallocate the cursor
CLOSE tables
DEALLOCATE tables

select * from #tables order by 2 desc

No comments:

Post a Comment