more number of rows in database

Vineet S 1,070 Reputation points
2024-07-31T11:56:17.3+00:00

HI,

How to get view which has more number of rows in to it in sql server database

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
14,013 questions
Not Monitored
Not Monitored
Tag not monitored by Microsoft.
39,772 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,657 questions
0 comments No comments
{count} votes

Accepted answer
  1. LiHongMSFT-4306 28,121 Reputation points
    2024-08-01T02:57:04.63+00:00

    Hi @Vineet S

    Try this:

    DECLARE @sql VARCHAR(MAX)
    DECLARE @view_elements VARCHAR(MAX)
    
    SELECT @view_elements = ISNULL( @view_elements ,'') + Part
    FROM (SELECT CONCAT(' UNION ALL ','select ','''',s.name,'.',v.name,'''',',', 'count(*) from ',s.name,'.',v.name) AS Part
          FROM sys.views v INNER JOIN sys.schemas s ON s.schema_id = v.schema_id) S
    GROUP BY Part
    --PRINT @view_elements
    
    SET @sql = ';WITH CTE AS ( SELECT '''' AS ViewName, 0 AS CountRows'+ @view_elements +') SELECT TOP(3) * FROM CTE WHERE CountRows > 40 ORDER BY CountRows DESC'
    --PRINT (@sql)
    
    EXEC (@sql)
    

    Best regards,

    Cosmog


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Erland Sommarskog 112.7K Reputation points MVP
    2024-07-31T21:33:44.6633333+00:00
    CREATE TABLE #views(viewname nvarchar(256) PRIMARY KEY, cnt int NOT NULL)
    DECLARE @sql nvarchar(MAX)
    SELECT @sql = string_agg(convert(nvarchar(MAX), 'INSERT #views(viewname, cnt) SELECT ' + 
                             quotename(s.name + '.' + v.name, '''') + ', COUNT(*) 
                             FROM ' + quotename(s.name) + '.' + quotename(v.name)), char(13) + char(10))
    FROM   sys.views v
    JOIN   sys.schemas s ON v.schema_id = s.schema_id
    
    PRINT @sql
    EXEC (@sql)
    
    SELECT TOP 3 viewname, cnt
    FROM   #views
    ORDER  BY cnt DESC
    go
    DROP TABLE #views
    
    
    1 person found this answer helpful.
    0 comments No comments

  2. Zahid Butt 956 Reputation points
    2024-07-31T12:10:04.83+00:00

    Hi,

    Please use below query in SSMS & scroll down to view all rows in your table:

    select * from TableName

    If otherwise pls describe your requirement in detail.

    Regards,


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.