Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Recompiling all sp's and functions

Author  Topic 

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2008-10-09 : 05:46:42
Hi all,

does anyone know of a way to recompile all the sp's and functions on a database (or at least check all the dependancies etc).

I need to make a lot of changes to interlocked sp's and need to know that everything is still going to be OK afterwards.

I know exec sp_recompile will mark the objects for recompilation next time they are run but I actually need something that will recompile them now and go through the usual checks.

Thanks in advance.

-------------
Charlie

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-09 : 05:50:34
i think you can use sp_depends to get dependencies of an object
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2008-10-09 : 05:55:08
Thanks Visakh,

I'm resigning myself to doing this for each sp change and checking the relevant objects.

(and then also going through all the sptext for the dynamic calls. Sigh)

-------------
Charlie
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2008-10-09 : 06:41:26
If anyone has a need to do this, I've written a little (very dangerous) script. I'd advise never to run this on a production environment.


DECLARE @object NVARCHAR(MAX)
DECLARE @text NVARCHAR(MAX)

IF OBJECT_ID('tempdb..#sqlText') IS NOT NULL DROP TABLE #sqlText

CREATE TABLE #sqlText (
[Id] INT IDENTITY(1,1)
, [line] NVARCHAR(255)
)

DECLARE recCursor CURSOR LOCAL READ_ONLY FOR
SELECT
[name]
FROM
sysObjects
WHERE
[xType] IN ('P', 'FN')

OPEN recCursor

FETCH NEXT FROM recCursor INTO @object

WHILE ( @@FETCH_STATUS = 0 ) BEGIN

-- Comment out the print to only get the errors!
PRINT 'Recompile : ' + @object

SET @text = ''
TRUNCATE TABLE #sqlText

INSERT #sqlText EXEC sp_helpText @object

SELECT @text = @text + [Line] FROM #sqlText ORDER BY [Id] ASC

SELECT @text = REPLACE(@text, 'CREATE PROCEDURE', 'ALTER PROCEDURE')
SELECT @text = REPLACE(@text, 'CREATE PROCEDURE', 'ALTER PROCEDURE')
SELECT @text = REPLACE(@text, 'CREATE PROCEDURE', 'ALTER PROCEDURE')
SELECT @text = REPLACE(@text, 'CREATE FUNCTION', 'ALTER FUNCTION')
SELECT @text = REPLACE(@text, 'CREATE FUNCTION', 'ALTER FUNCTION')
SELECT @text = REPLACE(@text, 'CREATE FUNCTION', 'ALTER FUNCTION')


EXEC sp_executeSql @TEXT

FETCH NEXT FROM recCursor INTO @object

END

CLOSE recCursor
DEALLOCATE recCursor


-------------
Charlie
Go to Top of Page
   

- Advertisement -