Skip to content

Commit

Permalink
added spCheckObjects
Browse files Browse the repository at this point in the history
  • Loading branch information
capnsue committed Mar 9, 2023
1 parent 94001ee commit 28e691f
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
3 changes: 3 additions & 0 deletions schema/setVersion.sql
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ EXEC spCheckDBObjects
EXEC spCheckDBColumns
EXEC spCheckDBIndexes

-- check functions, sp's, and views against schema
EXEC spCheckObjects with recompile


-- now set the new version; this gets tweaked for each version
EXECUTE spSetVersion
Expand Down
69 changes: 69 additions & 0 deletions schema/sql/spCheckDB.sql
Original file line number Diff line number Diff line change
Expand Up @@ -596,5 +596,74 @@ GO
--


--==========================================================
if exists (select * from dbo.sysobjects
where id = object_id(N'[dbo].[spCheckObjects]')
and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[spCheckObjects]
GO

CREATE PROCEDURE [dbo].[spCheckObjects]
AS
BEGIN

/*
** start of the batch that checks the non-schemabound routines **
you use sp_refreshView for non-schema-bound views and spRefreshSQLModule for
non-schema-bound stored procedure, user-defined function, view, DML trigger,
database-level DDL trigger, or server-level DDL trigger.
*/
-- a couple of table variables, one to save the errors ....
DECLARE @Errors TABLE (TheOrder INT IDENTITY, [Description] NVARCHAR(255) NOT NULL);
-- ... and another for the list of non-schemabound routines or modules.
DECLARE @NonSchemaBoundRoutines TABLE
(
TheOrder INT IDENTITY PRIMARY KEY,
TheName sysname NOT NULL,
TheType sysname NOT NULL
);
-- we create a table with the name and type of each module
INSERT INTO @NonSchemaBoundRoutines (TheName, TheType)
SELECT Coalesce(QuoteName(Object_Schema_Name(object_id)) + '.', '')
+ QuoteName(name), Replace(Lower(type_desc), '_', ' ')
FROM sys.objects
WHERE type_desc IN
('VIEW', 'SQL_STORED_PROCEDURE', 'SQL_TABLE_VALUED_FUNCTION',
'SQL_INLINE_TABLE_VALUED_FUNCTION', 'SQL_TRIGGER', 'SQL_SCALAR_FUNCTION'
)
AND ObjectProperty(object_id, 'IsSchemaBound') = 0;
/* we now brazenly iterate through the table and, for each row, we pass
the name to the system procedure to refresh it */
DECLARE @ii INT, @iiMax INT; -- iterative variables
--initialise the two variables
SELECT @ii = 1, @iiMax = Max(TheOrder) FROM @NonSchemaBoundRoutines;
--now execute the sys.sp_refreshsqlmodule for each
DECLARE @MyModule sysname, @MyModuleType sysname;
WHILE @ii <= @iiMax
BEGIN
SELECT @MyModule = TheName, @MyModuleType = TheType
FROM @NonSchemaBoundRoutines
WHERE TheOrder = @ii;
BEGIN TRY
EXEC sys.sp_refreshsqlmodule @name = @MyModule;
END TRY
BEGIN CATCH
INSERT INTO @Errors ([Description])
SELECT 'The ' + @MyModuleType + ' ' + @MyModule
+ ' has a reference to an ' + Error_Message();
END CATCH;
SELECT @ii = @ii + 1;
END;
-- now report all the errors
SELECT TheOrder, [Description] FROM @Errors

END
GO


PRINT '[spCheckDB.sql]: Database checking procs created'
GO

0 comments on commit 28e691f

Please sign in to comment.