Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

My technology blog on SQL Server, TSQL, XML, FILESTREAM and other areas of SQL Server.
Browse by Tags · View All
XML 112
TSQL 69
XQuery 69
XQuery Functions 67
XQuery Training 65
XQuery in TSQL 64
XQuery Tutorial 63
SQL Server XQuery 63
XQuery-Labs 57
BRH 38

Archive · View All
September 2008 32
August 2008 30
July 2008 21
August 2009 19
June 2009 19
May 2010 18
January 2009 15
January 2010 14
October 2008 14
June 2008 13

TSQL - How to revalidate/refresh/recompile all stored procedures in a database?

Jan 28 2009 2:02PM by Jacob Sebastian   

When you create/alter a stored procedure, SQL Server will perform a certain set of validations and will throw an error if the validation fails. After a stored procedure is created, you might make changes to the underlying objects that could break the stored procedure. The script presented in this post can be used to re-validate all stored procedures in a database.

We will use the system stored procedure, sp_refreshsqlmodule to revalidate each stored procedure. We will retrieve the list of stored procedures from system catalog view: sys.procedures. Here is the script that performs this.

-- table variable to store procedure names
DECLARE @v TABLE (RecID INT IDENTITY(1,1), spname sysname)

-- retrieve the list of stored procedures
INSERT INTO @v(spname)
SELECT
'[' + s.[name] + '].[' + sp.name + ']'
FROM sys.procedures sp
INNER JOIN sys.schemas s ON s.schema_id = sp.schema_id
WHERE is_ms_shipped = 0

-- counter variables
DECLARE @cnt INT, @Tot INT
SELECT @cnt = 1
SELECT @Tot = COUNT(*) FROM @v

DECLARE @spname sysname

-- start the loop
WHILE @Cnt <= @Tot BEGIN
SELECT @spname = spname
FROM @v
WHERE RecID = @Cnt

PRINT 'refreshing...' + @spname

BEGIN TRY
-- refresh the stored procedure
EXEC sp_refreshsqlmodule @spname
END TRY
BEGIN CATCH
PRINT 'Validation failed for : ' +
@spname + ', Error:' +
ERROR_MESSAGE()
END CATCH
SET @Cnt = @cnt + 1
END

The first part of the code inserted the names of all stored procedures to a table variable, along with their schema names. A table variable is used just to avoid a cursor. The WHILE loop then reads each stored procedure name and passes it to the system stored procedure: sp_refreshsqlmodule. sp_refreshsqlmodule  validates the stored procedure and will throw an error if the validation fails. The CATCH block catches the error if the validation fails, and displays the error message in the output window.

The above code will work only in SQL Server 2005 or above versions. I am eager to hear comments/suggestions to make this code better.

Tags: 


Jacob Sebastian
1 · 100% · 32004
0
Liked
 
0
Lifesaver
 
0
Refreshed
 
0
Learned
 
0
Incorrect



Submit

Your Comment


Sign Up or Login to post a comment.

    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]