USE [master]
GO
SET NOCOUNT ON;

DECLARE @db_name VARCHAR(100);
IF OBJECT_ID('tempdb..#objects')IS NOT NULL BEGIN;
    DROP TABLE #objects;
END;

DECLARE @filter_db NVARCHAR(100);
--SET @filter_db='ProductCompendium_Test'
SET @filter_db='Reporting_Work'

CREATE TABLE #objects ([object_name] varchar(100), [schema_name] varchar(50), [object_type] varchar(50));
DECLARE @objects_error TABLE ([db_name] VARCHAR(100), [schema_name] VARCHAR(100), [object_name] varchar(100), [object_type] VARCHAR(100), [error_msg] VARCHAR(MAX), [cmd] NVARCHAR(MAX));
DECLARE @object_name varchar(100);
DECLARE @schema_name varchar(100);
DECLARE @object_type varchar(100);
DECLARE @sql NVARCHAR(MAX);
DECLARE @output_only BIT = 0;

DECLARE csr_dbs CURSOR FAST_FORWARD READ_ONLY FOR
    SELECT d.name
    FROM sys.databases d
    WHERE d.name not in ('master','model','msdb','tempdb')
    AND [d].[source_database_id] IS NULL
    AND [d].[is_read_only] = 0
    AND [d].[is_in_standby] = 0
    AND COALESCE(@filter_db, d.[name]) = [d].[name]

OPEN csr_dbs

FETCH NEXT FROM csr_dbs INTO @db_name

WHILE @@FETCH_STATUS = 0
BEGIN
    --fetch objects
    SET @sql = '
USE '+@db_name+'

    DELETE FROM #objects;
    
    INSERT INTO #objects([object_name], [schema_name], [object_type])
    SELECT
        name as [object_name]
        , SCHEMA_NAME([schema_id]) AS [schema_name]
        , [type_desc] as [object_type]
    FROM sys.[objects] o
    WHERE [type_desc] IN (
        ''VIEW''
        ,''SQL_TABLE_VALUED_FUNCTION''
        ,''SQL_INLINE_TABLE_VALUED_FUNCTION''
        ,''SQL_SCALAR_FUNCTION''
        --,''CLR_TABLE_VALUED_FUNCTION''
        --,''CLR_SCALAR_FUNCTION''
        ,''SQL_STORED_PROCEDURE''
        ,''SQL_TRIGGER''
    )
    AND NOT EXISTS (
        SELECT 1
        FROM [sys].[sql_expression_dependencies] d
        WHERE d.[referencing_id] = o.[object_id]
        AND [d].[is_schema_bound_reference] = 1
    );

'
    EXEC sp_executesql @sql, N'';

    DECLARE csr_object CURSOR FAST_FORWARD READ_ONLY FOR
        SELECT [object_name], [schema_name], [object_type]
        FROM #objects
    
    OPEN csr_object
    
    FETCH NEXT FROM csr_object INTO @object_name, @schema_name, @object_type
    
    WHILE @@FETCH_STATUS = 0
    BEGIN
        BEGIN TRY
            SELECT @sql='USE '+@db_name+'; EXEC sp_refreshsqlmodule ''['+@schema_name+'].['+@object_name+']''; ';
            IF @output_only = 0
            BEGIN
                --PRINT @db_name+'.'+@schema_name+'.'+@object_name
                EXEC sp_executesql @sql, N'';
            END
            ELSE
            BEGIN
                PRINT @sql;
            END
        END TRY
        BEGIN CATCH
            DECLARE @errorMsg VARCHAR(MAX);
            SELECT @errorMsg = error_message();

            IF NOT @errorMsg LIKE '%is being referenced%'
            BEGIN

                INSERT INTO @objects_error([db_name], [schema_name], [object_name], [error_msg], [object_type], [cmd])
                SELECT @db_name, @schema_name, @object_name, @errorMsg, @object_type, @sql;

                PRINT 'issues in db "'+@db_name+'" object (type '+@object_type+')"'+@schema_name+'.'+@object_name+'": '+ERROR_MESSAGE()
                PRINT @sql

            END
        END CATCH
    
        FETCH NEXT FROM csr_object INTO @object_name, @schema_name, @object_type
    END
    
    CLOSE csr_object
    DEALLOCATE csr_object
    FETCH NEXT FROM csr_dbs INTO @db_name
END

CLOSE csr_dbs
DEALLOCATE csr_dbs

SELECT *
FROM @objects_error
ORDER BY [db_name], [schema_name], [object_name];
