I want some Moore

Blog about stuff and things and stuff...
mostly about SQL server and .Net
posts - 161, comments - 1491, trackbacks - 33

My Links

SQLTeam.com Links

News

Hi! My name is 
Mladen Prajdić  I'm from Slovenia and I'm currently working as a .Net (C#) and SQL Server developer. I'm also a MCP and MCTS for SQL Server. 
Welcome to my blog.

Search this Blog
 

My Blog Feed via Email


Get your Google PageRank
Users Online: who's online

Article Categories

Archives

Post Categories

Cool software

Other Blogs

Other stuff

SQL stuff

SQL Server: How to run a stored procedure at SQL Server start-up

This is acctually quite simple. There is 'startup' option that you can set to the procedure.

There are a few limitations though:

 - your stored pricedure must reside in the [master] database

 - it's owner must be dbo

 - it musn't have any input or output parameters

 

Here is some sample code: 

USE master;
GO
-- first set the server to show advanced options
EXEC sp_configure 'show advanced option', '1';
RECONFIGURE
-- then set the scan for startup procs to 1
EXEC sp_configure 'scan for startup procs', '1';
RECONFIGURE

IF OBJECT_ID('spTest') IS NOT NULL
    DROP PROC spTest
GO
-- crate a test stored procedure
CREATE PROC spTest
AS
-- just create a sample database
EXEC('CREATE database db1')

GO
-- set it to run at sql server start-up
exec sp_procoption N'spTest', 'startup', 'on'

 

Note that each stored procedure run at start up takes up one worker thread until finished. So if you want to run multiple sprocs at runtime

and parallelism doesn't matter create one sproc that executes all others.

 

kick it on DotNetKicks.com

Print | posted on Tuesday, August 14, 2007 4:34 PM

Feedback

# re: SQL Server: How to run a stored procedure at SQL Server start-up

its nice...
if stored procedure return the value like function?
11/23/2007 12:12 PM | Malaravan

# re: SQL Server: How to run a stored procedure at SQL Server start-up

Very useful, thanks!
1/16/2008 10:07 AM | riaz

# re: SQL Server: How to run a stored procedure at SQL Server start-up

Question - if this is a startup procedure, does it run BEFORE anything else gets in?
2/20/2008 3:50 PM | LinuxLars

# re: SQL Server: How to run a stored procedure at SQL Server start-up

and by anything else you mean what?
2/20/2008 3:54 PM | Mladen

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 2 and 5 and type the answer here:

Powered by: