-- 创建存储过程,每隔 30 秒钟运行一次。
create procedure usp_daemon
as
set nocount on
while 1=1
begin
update [user] set type=0 where data<getdate();
delete from [tab] where userid in (select userid from [user] where type=0);
waitfor delay '00:00:30'
end
go
-- 将 sql server 实例设置为“在实例启动时扫描可自动执行的存储过程”
sp_configure 'show advanced options',1
reconfigure
sp_configure 'scan for startup procs',1
reconfigure
sp_configure 'show advanced options',0
reconfigure
-- 将存储过程设置为“实例启动时自动执行”
sp_procoption 'usp_daemon','startup','true'
[MSSQL]sql 存储过程定时执行方法
最新推荐文章于 2024-12-30 17:02:44 发布
本文介绍了一个SQL Server存储过程,该过程被配置为每隔30秒自动执行一次,用于更新用户状态并清理相关表数据。此外,还介绍了如何配置SQL Server实例以在启动时自动扫描并执行此存储过程。
1904

被折叠的 条评论
为什么被折叠?



