Run a Stored Procedure when SQL Server starts

Although it is not a very good idea, you can set up a stored procedure to be started when SQL Server starts. Follow these steps:

Step 1: When you are creating the Stored Procedure, make sure the database context is set to Master database

USE master
GO
CREATE PROCEDURE dbo.usp_SomeProcForStart
AS
-- Stored Procedure Definition
GO


Note that your stored procedure cannot have any parameters and the owner is ‘dbo’

Step 2: The next step is to use the sp_procoption to set the stored procedure dbo.usp_SomeProcForStart to execute when SQL Server service starts:

EXEC sp_procoption 'usp_SomeProcForStart', 'startup', 'true'

To turn it off so that this stored procedure ‘does not’ execute when SQL Server service starts, use this command

EXEC sp_procoption 'usp_SomeProcForStart', 'startup', 'false'


Note: You may also want to take a look at sp_configure system stored proc in case your database is not configure for automatic execution of stored procedures.


About The Author

Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of DotNetCurry, DNC Magazine for Developers, SQLServerCurry and DevCurry. He has also authored a couple of books 51 Recipes using jQuery with ASP.NET Controls and a new one recently at The Absolutely Awesome jQuery CookBook.

Suprotim has received the prestigous Microsoft MVP award for nine times in a row now. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that represents premium web sites and digital publications comprising of Professional web, windows, mobile and cloud developers, technical managers, and architects.

Get in touch with him on Twitter @suprotimagarwal, LinkedIn or befriend him on Facebook

No comments: