如何从数据库中获取视图的行数?

时间:2012-01-10 19:24:23

标签: sql sql-server-2008-r2

我正在尝试创建一个自定义视图,以获取数据库所有视图和表的行数。

在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  

现在,我需要获取VIEWS的行数

我觉得唯一的方法是计算视图中的行数 即count(*) from view_name

但是,我找不到一种方法可以将viewcount与view_name,table_schema等一起使用。

任何进展都会有所帮助。

4 个答案:

答案 0 :(得分:0)

嗯,它不是很漂亮,但这应该可以解决问题。

这适用于单个数据库,因为我使用系统视图'all_views'来获取数据。如果你想要别的东西,我相信它可以适应。

我还通过'schema_id'限制了我要返回的视图。您可以查看此内容并确定最适合您的方式。

最后,我只是吐出结果,以便您在Management Studio中运行它时可以查看它们。您可能希望将结果插入到表格或其他内容中。如果是这样,只需将动态查询从选择更改为插入即可。

所以没有进一步的说明:

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

答案 1 :(得分:0)

这是最终的解决方案:

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

答案 2 :(得分:0)

我知道这是一篇旧帖子,我不打算从标记的答案中获取信用,但想加入它。

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

我添加了视图名称以帮助区分更多数字。

答案 3 :(得分:0)

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