IF OBJECT_ID(N'AUDIT_REC_COUNT', N'U') IS NOT NULL
DROP TABLE AUDIT_REC_COUNT;
GO
CREATE TABLE [AUDIT_REC_COUNT](TABLENAME varchar(50), [COUNT] NUMERIC)
GO
DECLARE @Count TABLE (
[TABLENAME] VARCHAR(20) COLLATE SQL_Latin1_General_CP1_CS_AS NOT NULL ,
[COUNT] FLOAT NULL)
DECLARE @tablename AS VARCHAR(50)
DECLARE Record_Count CURSOR FOR
SELECT [NAME] [ATTRIB] FROM sys.tables
OPEN Record_Count
FETCH NEXT FROM Record_Count
INTO @tablename
WHILE @@FETCH_STATUS = 0
BEGIN
exec('insert into AUDIT_REC_COUNT(TABLENAME, [COUNT])
select '''+@tablename+''', count(*) from '+@tablename+'')
FETCH NEXT FROM Record_Count
INTO @tablename
END
CLOSE Record_Count
DEALLOCATE Record_Count
GO
ALTER TABLE AUDIT_REC_COUNT
ADD TYPE varchar(50)
GO
UPDATE AUDIT_REC_COUNT SET TYPE =
(CASE WHEN TABLENAME LIKE '%_fallback_%' THEN 'fallback'
WHEN TABLENAME LIKE '%_monitor%' THEN 'monitor'
ELSE NULL END
)
GO
SELECT * FROM [AUDIT_REC_COUNT] WHERE count > 0
ORDER BY TABLENAME
|