How to get the row count for views from a database?

柔情痞子 提交于 2019-12-06 15:53:41

Well, it isn't very pretty, but this should do the trick.

This applies to a single database because I'm using the system view 'all_views' to get the data. I'm sure it could be adapted if you wanted something else.

I've also limited the views I'm returning counts for, via the 'schema_id'. You can look at this and determine what would be best for you.

Lastly, I've just spit the results out so that you can view them if you run this in Management Studio. You would probably want to insert the results into a table or something instead. If so, it's simply a matter of changing the dynamic query from a select to an insert.

So without further adieu:

SET NOCOUNT ON

DECLARE   @ViewName AS nVarChar(128)
        , @Query AS nVarChar(500)

/* Declare Cursor */
DECLARE Cur_Views CURSOR
    FOR
    SELECT  name
        FROM    [sys].[all_views] x
        WHERE   x.schema_id = 1

-- Loop through the views.
OPEN Cur_Views

-- Fetch the first view
FETCH NEXT FROM Cur_Views
    INTO      @ViewName

WHILE   @@Fetch_Status = 0 BEGIN
    -- Set up our dynamic sql
    SELECT  @Query = 'SELECT COUNT(*) AS [Count] FROM ' + @ViewName
    -- Print the query we're executing for debugging purposes
    -- PRINT @Query
    -- Execute the dynamic query
    EXECUTE(@Query)

    -- Fetch subsequent views
    FETCH NEXT FROM Cur_Views
        INTO      @ViewName

-- Loop back to the beginning
END -- WHILE    @@Fetch_Status = 0 BEGIN

-- Close the cursor
CLOSE Cur_Views

-- Dispose of the cursor
DEALLOCATE Cur_Views

GO

Here , is the final solution:

SET NOCOUNT ON
DECLARE   @ViewName AS nVarChar(128)
    , @TmpQuery AS nVarChar(500)
    , @Out3 as int
DECLARE Cur_Views CURSOR FOR
SELECT schema_name(schema_id)+'.'+name as "Table_Name" FROM [sys].[all_views] 
OPEN Cur_Views
FETCH NEXT FROM Cur_Views INTO @ViewName
WHILE   @@Fetch_Status = 0 BEGIN
--SELECT  @Query = 'SELECT COUNT(*) AS [Count] FROM ' + @ViewName
--EXECUTE(@Query)
CREATE TABLE #Data (var int)
SELECT  @TmpQuery = 'SELECT COUNT(*) AS [Count] FROM ' + @ViewName
INSERT #Data exec (@TmpQuery)
SELECT @Out3 = var from #Data
--PRINT @ViewName
--PRINT @Out3
insert into Person.ViewCountTracker values(@ViewName,@Out3)
DROP TABLE #Data
FETCH NEXT FROM Cur_Views INTO @ViewName
END
CLOSE Cur_Views
DEALLOCATE Cur_Views
GO
--create table Person.ViewCountTracker
--(
--  ViewName varchar(255),
--  RowValue int
--)
--select * from Person.ViewCountTracker

I know this is an old post and I do not intend to take credit from the marked answer but wanted to add to it.

SELECT  @Query = 'SELECT ''' + @ViewName + ''' AS Name, COUNT(*) AS [Count] FROM ' + @ViewName

I added the View name to help distinguish the numbers more.

SET NOCOUNT ON
DECLARE @ViewName AS nVarChar(128), @TmpQuery AS nVarChar(384)
CREATE TABLE #Results (Name nVarChar(128), Cnt BigInt)
DECLARE Cur_Views CURSOR FOR SELECT schema_name(schema_id) + '.' + name AS Name FROM [sys].[all_views] WHERE is_ms_shipped = 0
OPEN Cur_Views
WHILE  (1=1)
BEGIN
    FETCH NEXT FROM Cur_Views INTO @ViewName
    If @@Fetch_Status != 0 BREAK
    SELECT  @TmpQuery = 'SELECT ''' + @ViewName + ''' AS Name, COUNT_BIG(*) AS Cnt FROM ' + @ViewName + ' (NoLock)'
    PRINT @TmpQuery
    INSERT #Results EXEC (@TmpQuery)
END
CLOSE Cur_Views
DEALLOCATE Cur_Views
SET NOCOUNT OFF
SELECT * FROM #Results
DROP TABLE #Results
GO
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!