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 2000 Forums
 SQL Server Administration (2000)
 recompile stored procedures

Author  Topic 

Hippi
Yak Posting Veteran

63 Posts

Posted - 2005-06-27 : 22:32:48
Hi folks,

I need to recompile some stored procedures. I've read somewhere that after we run sp_recompile on some SP, SQL server will mark that SP so that it will recompile on next run.
Do you guys know where SQL server store that information so that it will know which SP to recompile on the next run?

Thanks a buch,
Hippi.

derrickleggett
Pointy Haired Yak DBA

4184 Posts

Posted - 2005-06-27 : 23:00:42
The dbo.syscacheobjects table will hold the information you are looking for if you want to "verify" the recompile for some reason. Basically, when you run an sp_recompile, you are simply removing the cached and executable plan from cache. If you look at the syscacheobjects table, you will see this.

For example, if you have the following query:


SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO

/****** Object: Stored Procedure dbo.folder_contents_get Script Date: 6/27/2005 9:57:31 PM ******/
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[folder_contents_get]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[folder_contents_get]
GO


CREATE PROCEDURE folder_contents_get

--Name: folder_contents_get
--
--Purpose: Keep folder information stored, including addition and deletion dates of files.
-- Run as a scheduled job daily. Keeps folder contents updated for SQL Server jobs.
--
--Notes: Must use "" around folder names with multiple words. See Example_1.
--
--Type: Utility
--
--Format: EXEC sa_ProcessInformation @folder_name
--
--Example_1: EXEC folder_contents_get 'C:\"Documents and Settings"\dleggett' SELECT * FROM folder_contents
--
--Author: Date: Type: Description:
--Derrick Leggett 01/13/2003 Created Created to help server inventory project.
--

@folder_name VARCHAR(256)

AS

DECLARE
@rundate DATETIME,
@dir NVARCHAR(2000)

SELECT @rundate = GETDATE()

--Create temp table to hold contents of dir.
CREATE TABLE #contents(folder_file VARCHAR(256))

--Format dir string properly.
SELECT @dir = 'DIR ' + @folder_name + ' /B /A:-D'

-- Get the contents. This will error if they didn't pass a valid directory name.
INSERT #contents(folder_file)
EXEC master..xp_cmdshell @dir

IF @@ERROR <> 0
BEGIN
RAISERROR('Invalid directory name.',16,1)
END

--Insert new records into the folder_contents table.
INSERT folder_contents(
folder_name,
folder_file,
date_added,
date_deleted)

SELECT
@folder_name,
c.folder_file,
@rundate,
NULL
FROM
#contents c
LEFT OUTER JOIN (
SELECT
folder_name,
folder_file,
MAX(ISNULL(date_deleted,'12/31/2100')) date_deleted
FROM folder_contents
GROUP BY
folder_name,
folder_file) fc ON c.folder_file = fc.folder_file
AND fc.folder_name = @folder_name
WHERE
(fc.folder_file IS NULL
OR fc.date_deleted < '12/31/2100')
AND c.folder_file IS NOT NULL

--Update the deleted files.
UPDATE fc
SET date_deleted = @rundate
FROM
folder_contents fc
LEFT OUTER JOIN #contents c ON fc.folder_file = c.folder_file
AND fc.folder_name = @folder_name
WHERE
fc.date_deleted IS NULL
AND c.folder_file IS NULL

GO

SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO



When you run it the first time, it puts a plan in cache. If you keep running it with the same parameters, it will use that same plan repeatedly. Your usecounts will increase with each hit. If you recompile the query, you will have not entries in syscacheobjects because the plan no longer exists.

Note: This is from the research I have done personally. So....it would be nice to see an MS employee blow my theory away or confirm it.

MeanOldDBA
derrickleggett@hotmail.com

When life gives you a lemon, fire the DBA.
Go to Top of Page
   

- Advertisement -