Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8622223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:59:56+00:00 2026-06-12T06:59:56+00:00

Question How do i loop though my list of views in my SQL DB?

  • 0

Question
How do i loop though my list of views in my SQL DB?

background and things tried
I have googled around for about 2 hours now and i have found quite a few ways to loop through different attributes of my DB. I can loop through a list of tables and a list of columns and a list of counts but i have yet to find anything that allows me to loop through a list of Views.

code to loop through tables i tried to modify

link i used to obtain sample code below

--List all the tables of current database and total no rows in it
EXEC sp_MSForEachTable 'SELECT ''?'' as TableName, COUNT(1) 
as TotalRows FROM ? WITH(NOLOCK)' 

--List all the tables of current database and space used by it EXECUTE sp_MSforeachtable 'EXECUTE sp_spaceused [?];'; 
GO

sudo code for what i want to do

loop through each view in list
run query here for each view
output results

request for help

Can anyone link me to more information about how to do this or provide some sample code? All help is greatly appreciated! Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T06:59:58+00:00Added an answer on June 12, 2026 at 6:59 am

    There are a lot of ways to do this, cursors, manual loops. There are likely ways to find what you want (meaning the answer to your mystery query) in a set based way. Either way, as phrased you’ll need dynamic sql.

    Option 1 – a so/so method

    SET NOCOUNT ON;
    DECLARE @Views as TABLE (Object_id int, name nvarchar(200));
    
    INSERT INTO @Views (Object_ID, name)
    SELECT      Object_ID, name 
    FROM        sys.views
    
    
    DECLARE @viewName   nvarchar(200) = (select top 1 name from @Views);
    DECLARE @sql        nvarchar(max) = '';
    
    WHILE(Exists(select 1 from @Views)) BEGIN
        SET @sql = 'select count(*) FROM ' + @ViewName +';'
        --exec(@sql);   --careful you're not accepting user input here!
        Print (@sql);
    
        DELETE FROM @Views where name = @viewName;
        SET @ViewName = (select top 1 name from @Views);
    END;
    

    Option 2 – A More Resilient Method
    While trying this out in my little test DB I realized my first solution (as well as all the others) have a potential issue. Views can become out of date –meaning underlying objects change like a column rename. All of the others solutions will throw an error and stop processing. If you’re doing large/long queries this may be undesirable. So I added a smidge of error handling to note the error and continue on processing.

    SET NOCOUNT ON;
    DECLARE @ViewCount   int                = 0;
    DECLARE @Counter     int                = 0;
    DECLARE @sql         nvarchar(max)    = '';
    DECLARE @viewName    nvarchar(200)    = ''
    DECLARE @Views    as TABLE (  rownum int identity(1,1), 
                                  name nvarchar(200), 
                                  Primary Key clustered  (rownum)
                                );
    
    INSERT INTO  @Views (name)
    SELECT       name 
    FROM         sys.views;
    
    SET          @ViewCount = SCOPE_IDENTITY();
    
    WHILE(@Counter < @ViewCount) BEGIN
        SET @Counter = @Counter+1;
    
        SELECT  @sql = 'select count(*) FROM ' + name +';', @viewName = name
        FROM    @Views
        WHERE    rownum = @Counter;
    
        BEGIN TRY
            -- exec(@sql); --careful you're not accepting user input here!
            Print (@sql);
        END TRY BEGIN CATCH
            Print ('ERROR querying view - ' + @viewname + ' // ' + ERROR_MESSAGE());
        END CATCH
    END;    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a nice question about the loop-expression of a for-loop in C#. I
I have loop designed to validate the user input on a question, it was
er,here is a question about netty nioWorker Netty architecture - questions about NioWorker loop
Hi I have a quick question, which is the easiest way to loop inside
First time poster here. A quick question about setting up a loop here. I
So i have 2 questions about this ArrayAdapter : 1. the third parameter List<RSSItem>
I have been at this for several days now and tried all sorts of
I have a simple piece of Scala code. I loop sequentially through a List
Sorry if this is a dumb question. I'm using .each to loop through a
Seeing various locking related question and (almost) always finding the 'loop because of spurious

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.