Query a list of objects in each database I have a list of about 300 Views spread out across several databases.
Some of the tables used in the DDL of the views have been dropped or renamed.
What I want to do is try to query all 300 views in each database.
If a query is successful, I want to output &quot;Query Successful&quot;.
If a query fails because the object the view DDL is based on fails, I want to output &quot;Query Failed&quot;.
The final output will look something like this
database_name     schema_name     view_name     status
db_1              dbo             view_1        Query Successful
db_2              abc             view_2        Query Failed

I know part of the solution will use the sp_MSforeachdb stored procedure, but I'm not sure what to do after that.
I have a list of all the objects I want to query stored in a table
You can use the following code to list all views against all databases.
It will populate everything inside of #Result. After that, you can use your list to do joins and check whatever status you want.
IF OBJECT_ID('tempdb..#Result') IS NOT NULL  DROP TABLE #Result;
CREATE TABLE #Result
([Database] VARCHAR(100), [schema_name] VARCHAR(100), [view_name] VARCHAR(100))

DECLARE @DB SYSNAME,@cmd NVARCHAR(4000)

SET NOCOUNT ON
SET @cmd = ''

SELECT TOP 1 @DB = D.name
FROM master..sysdatabases AS D
WHERE dbid &gt; 4
    and DATABASEPROPERTYEX(D.name, 'status') = 'ONLINE'
ORDER BY D.name;

WHILE @@ROWCOUNT = 1
BEGIN
      SET @cmd = 'USE ' + @DB + ';
            INSERT INTO #Result
            SELECT  DISTINCT ''' + @DB + ''' AS CustomerNumber ,sh.name, o.name AS ObjectName
            FROM sys.objects o 
            INNER JOIN sys.schemas sh on o.schema_id=sh.schema_id
            WHERE o.type = ''V''

    ';
      EXEC (@cmd)

      SELECT TOP 1 @DB = NAME
      FROM master..sysdatabases AS D
        WHERE dbid &gt; 4
        AND DATABASEPROPERTYEX(D.name, 'status') = 'ONLINE'
            AND D.NAME &gt; @DB
      ORDER BY D.name
END

SELECT * FROM #Result R