sql server 服务启动时自动执行某个存储过程

1.将存储过程创建在master数据库中,注意存储过程不能有参数

2.执行下面语句设置服务启动时自动执行

use master
exec sp_procoption '存储过程名','startup','on'

3.取消服务启动时自动执行

use master

exec sp_procoption '存储过程名','startup','off'

例子:

USE [master]
GO
/****** Object:  StoredProcedure [dbo].[p_a]    Script Date: 2022/7/6 9:50:52 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:        toazi
-- Create date: 2022年7月6日09点51分
-- Description:    sqlserver服务重启后将自动执行该存储过程
-- =============================================
ALTER PROCEDURE [dbo].[p_a] 
AS
BEGIN
    SET NOCOUNT ON;

    declare @start_time1 datetime = getdate(),@used_time1 int

    select * from [TCX_demo].[dbo].[t_dd_qm_product_material]
    select * from [TCX_demo].[dbo].[t_dd_qm_product_testitem]
    select * from [TCX_demo].[dbo].[t_dd_qm_quality_collection_final]
    select * from [TCX_demo].[dbo].[t_dd_qm_station_collect]

    set @used_time1 = datediff(ms,@start_time1,getdate()); 
    insert into [TCX_demo].[dbo].[t_dd_bd_sql_log](id,create_date,key1,key2) values(replace(newid(),'-',''),getdate(),'服务启动执行查询用时记录',@used_time1);


END
use master
EXEC sp_procoption 'p_a','STARTUP','ON'

 

posted @ 2022-07-06 10:01  单纯的桃子  阅读(365)  评论(0编辑  收藏  举报