Created
February 13, 2019 10:05
-
-
Save Jalalx/f5c2238f48ad74d02fdace51d7539b24 to your computer and use it in GitHub Desktop.
Checks if all database objects are OK. Code obtained from https://stackoverflow.com/a/5555554/375958
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DECLARE @Name nvarchar(1000); | |
DECLARE @Sql nvarchar(1000); | |
DECLARE @Result int; | |
DECLARE ObjectCursor CURSOR FAST_FORWARD FOR | |
SELECT QUOTENAME(SCHEMA_NAME(o.schema_id)) + '.' + QUOTENAME(OBJECT_NAME(o.object_id)) | |
FROM sys.objects o | |
WHERE type_desc IN ( | |
'SQL_STORED_PROCEDURE', | |
'SQL_TRIGGER', | |
'SQL_SCALAR_FUNCTION', | |
'SQL_TABLE_VALUED_FUNCTION', | |
'SQL_INLINE_TABLE_VALUED_FUNCTION', | |
'VIEW') | |
--include the following if you have schema bound objects since they are not supported | |
AND ISNULL(OBJECTPROPERTY(o.object_id, 'IsSchemaBound'), 0) = 0 | |
; | |
OPEN ObjectCursor; | |
FETCH NEXT FROM ObjectCursor INTO @Name; | |
WHILE @@FETCH_STATUS = 0 | |
BEGIN | |
SET @Sql = N'EXEC sp_refreshsqlmodule ''' + @Name + ''''; | |
--PRINT @Sql; | |
BEGIN TRY | |
EXEC @Result = sp_executesql @Sql; | |
IF @Result <> 0 RAISERROR('Failed', 16, 1); | |
END TRY | |
BEGIN CATCH | |
PRINT 'The module ''' + @Name + ''' does not compile.'; | |
IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION; | |
END CATCH | |
FETCH NEXT FROM ObjectCursor INTO @Name; | |
END | |
CLOSE ObjectCursor; | |
DEALLOCATE ObjectCursor; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment