Automates the execution of stored procedures when SQL Server starts. 1th/2 page _mssql

Source: Internet
Author: User
SQL Server provides a system stored procedure sp_procoption that can be used to assign one or more stored procedures automatically when the SQL Server service is started. This is a good choice and it can be used for a variety of purposes. For example, you might have a large, expensive query in your database that will take some time to execute first. By using sp_procoption, you can run this query at server startup to precompile the execution plan so that one of your users will not be the first unfortunate person to run this particular query. I used this feature to create an automatic execution feature of the profiler's server-side tracking that I wrote myself, which became part of a stored procedure that was set to execute automatically when the server was started.
Copy Code code as follows:

Sp_procoption Parameters
exec sp_procoption @ProcName = [' Stored procedure name '],
@OptionName = ' STARTUP ',
@OptionValue = [On|off]

The following is an explanation of the sp_procoption stored procedure parameters:

The L parameter @procname meaning is obvious, it is the name of the procedure that is marked to be executed automatically.

L parameter @optionname is optional and the only valid option is startup.

L parameter @optionvalue Toggle automatic execution of the open and closed.

Use sp_procoption with specific restrictions:

L You must log on to the server as a system administrator and use sp_procoption.

L You can only assign standard stored procedures, extended stored procedures, or CLR stored procedures to start.

L stored procedures must be on the master-slave database.

L stored procedures cannot require any input parameters or return any output parameters.

In the following example, I create a stored procedure that automatically executes every time my instance of SQL Server is started. The purpose of this stored procedure is to write a row in the database table that records the service startup time. With this database table, I have an idea of getting the server to run normally. The following script creates a new database that stores a metric table called Server_startup_log. This table retains the date and time the last server was started. Once this basic architecture is established, I create a stored procedure that will be used to insert the date and time into the table when the server is started. Note that the stored procedure is created on the master-slave database.
Current 1/2 page 12 Next read the full text
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.