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

  • SEARCH
  • Home
  • 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 8706279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:34:36+00:00 2026-06-13T03:34:36+00:00

I am writing a stored procedure which iterates over all of the databases on

  • 0

I am writing a stored procedure which iterates over all of the databases on the server and populates a table variable with an aggregate of the data from some of the different databases. Some databases I’m not interested in as they are irrelevant. The problem is when my CURSOR iterates through those databases I don’t care about, a SELECT statement is issued on a table that doesn’t exist. How can I ignore the Invalid object name exception and continue with my processing?

Edit:

Here is how I was attempting to skip over databases that were irrelevant:

DECLARE db_cursor CURSOR FOR

SELECT name
FROM MASTER.dbo.sysdatabases
WHERE name NOT IN ('master','model','msdb','tempdb') 

OPEN db_cursor 

FETCH NEXT FROM db_cursor INTO @currentDatabaseName  

WHILE @@FETCH_STATUS = 0
    BEGIN
        SET @sql = 'SELECT COUNT(Name) FROM ' + @currentDatabaseName + '.sys.Tables WHERE Name = ''SomeTableICareAbout'''

        INSERT INTO @tableSearchResult
        EXEC sp_executesql @sql

        SET @tableCount = (SELECT COUNT(*) FROM @tableSearchResult WHERE TableCount = 1)

        --If the table I care about was found, then do the good stuff
        IF @tableCount > 0
            ...

The problem with this approach is if the executing user (in my case a service account) does not have access to SELECT on the table, then I never know about that error. If the user doesn’t have SELECT access, I want that exception to be raised. But, even if the user doesn’t have SELECT access, it can SELECT on the sys.Tables view.

  • 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-13T03:34:37+00:00Added an answer on June 13, 2026 at 3:34 am

    You can’t catch error 208 directly because it’s a name resolution error that is raised at compilation time and before the code is actually executed. The behaviour is documented: see the section called “Errors Unaffected by a TRY…CATCH Construct” for an explanation, and the answers to this question have some interesting comments.

    In addition to the ‘solution’ in the documentation, you can use dynamic SQL; the error will be caught in this example:

    begin try
        exec('select * from dbo.ThisTableDoesNotExist');
    end try
    begin catch
        select error_number();
    end catch;
    

    If you’re looping through all databases, there’s a good chance you’re using dynamic SQL somewhere anyway, so this might suit your case better.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a SQL Server stored procedure in which I want to lock a
I am working in SQL Server 2008. I am writing a stored procedure which
I am writing a stored procedure which collects all changes that were happend in
Hi I am writing a large stored procedure, which creates a dynamic report table,
I am writing a stored procedure, which opens a cursor to a table, and
I'm writing one stored procedure, which I have to create a xml column from
I am writing a stored procedure in SQL Server 2008. Here I have a
I am using SQL Server. I'm writing a stored procedure that executes a series
I'm writing a general search Stored Procedure to search in a table based on
I have a stored procedure which selects a single field from a single row

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.