How to execute a stored procedure when starting SQL server? Create the stored procedure in the master database, and then find the stored procedure in the Enterprise Manager. Right-click the stored procedure and choose Properties. Select "execute whenever SQL Server is started ". -- Or after the stored procedure is created in the master, the execution statement is set to automatic start. Use master Exec sp_procoption 'stored procedure name', 'startup', 'on' --------------------------------------------------------------- Automatic Execution of Stored Procedures When SQL Server is started, one or more stored procedures can be automatically executed. These stored procedures must be created by the system administrator and executed as background procedures under the SysAdmin fixed server role. These processes cannot have any input parameters. There is no limit on the number of startup processes, but note that each Startup Process occupies a connection during execution. If multiple processes must be executed at startup without parallel execution, you can specify a process as the startup process to call other processes. In this way, only one connection is occupied. After the last database is restored at startup, the stored procedure starts. To skip the execution of these stored procedures, set the startup parameter to trace tag 4022. If SQL Server is started with the lowest configuration (with the-F flag), the stored procedure will not be started. For more information, see trace tag. To create a stored procedure, you must log on as a member of the SysAdmin fixed server role and create a stored procedure in the master database. You can use sp_procoption: Specify an existing stored procedure as a startup process. The blocking process is executed when SQL Server is started. |