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

亡梦爱人 提交于 2019-12-22 17:52:54

问题


I'm trying to make a customized view that gets me the row count for all the views and tables of a database.

Getting the count of table is damn in SQL Server

SELECT TABLE_SCHEMA,
       TABLE_NAME = TABLES.TABLE_NAME,
       RECORD_COUNT = MAX(SYSINDEXES.ROWS)
FROM   SYS.SYSINDEXES "SYSINDEXES",
       INFORMATION_SCHEMA.TABLES "TABLES"
WHERE  TABLES.TABLE_NAME = OBJECT_NAME(SYSINDEXES.ID)
       AND TABLES.TABLE_TYPE = 'BASE TABLE'
GROUP  BY TABLES.TABLE_SCHEMA,
          TABLES.TABLE_NAME  

Now, I need to get the rowcount for VIEWS

I feel the only way is to count the number of rows from the views i.e. count(*) from view_name

But, I could not find a way to have the rowcount for view along with view_name, table_schema and so on.

Any advance on this would be helpful.


回答1:


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



回答2:


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



回答3:


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.




回答4:


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


来源:https://stackoverflow.com/questions/8809263/how-to-get-the-row-count-for-views-from-a-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!