0

I need to execute a request (Stored Procedure) to MS SQL Server after Windows restart, i.e. as soon as the database is accessible.

How can I do that? Is there a possibility to register a database event that fires after startup or a system event after a service started (here: SQL Server Service)?

1

2 Answers 2

4

You can use a Startup Stored Procedure or schedule a SQL Agent with a Schedule type of "Start automatically when SQL Server Agent Starts".

2

There is a system stored procedure named sp_procoption that does exactly that. See

https://learn.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp-procoption-transact-sql?view=sql-server-ver15

Syntax is fairly straight forward. If you have a procedure named 'my_stored_proc', the syntax is:

EXEC sp_procoption @ProcName = N'my_stored_proc'   
, @OptionName = 'startup'   
, @OptionValue = 'on';   

The only value for @OptionName is 'startup', and the only possible values for @OptionValue are 'on' and 'off.'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.