简体   繁体   English

SQL Server:以编程方式进行计划

[英]SQL Server : scheduling programmatically

I'm trying to force SQL Server to call a needed stored procedure each hour. 我试图强制SQL Server每小时调用一次所需的存储过程。

I've read the following articles: 我已阅读以下文章:

They are quite big and not very straight forward. 他们很大,不是很直接。

Can anyone confirm that the following SQL code is right for my task: 任何人都可以确认以下SQL代码适合我的任务:

USE msdb ;
GO
EXEC dbo.sp_add_job
    @job_name = N'Exec RemoveOldCsvImportData' ;
GO
EXEC sp_add_jobstep
    @job_name = N'Exec RemoveOldCsvImportData',
    @step_name = N'execute stored procedure',
    @subsystem = N'TSQL',
    @command = N'exec RemoveOldCsvImportData', 
    @retry_attempts = 5,
    @retry_interval = 5 ;
GO
EXEC dbo.sp_add_schedule
    @schedule_name = N'RunOnce',
    @freq_type = 4,
    @freq_interval = 1,
    @freq_subday_type = 0x8,
    @freq_subday_interval = 1,
    @active_start_time = 233000 ;
USE msdb ;
GO
EXEC sp_attach_schedule
   @job_name = N'Exec RemoveOldCsvImportData',
   @schedule_name = N'RunOnce';
GO
EXEC dbo.sp_add_jobserver
    @job_name = N'Exec RemoveOldCsvImportData';
GO 

My suggestion would be to create the job through the wizard, script it out, and then use that for your script. 我的建议是通过向导创建作业,将其编写脚本,然后将其用于您的脚本。 Creating a job like you're trying to do is an arcane process. 像您尝试做的那样创建工作是一个神秘的过程。 You'll get it eventually, but if this is a one-shot, the wizard route will save you a lot of time. 您最终会得到它的,但是,如果这是一次尝试,向导路线将为您节省大量时间。

Apart from the spurious USE msdb ; 除了虚假的USE msdb ; in the middle, it looks good, and it runs successfully. 在中间,它看起来不错,并且运行成功。 Why not just try running it? 为什么不尝试运行它呢? You can always delete the job again if it's not quite what you want. 如果不是您想要的工作,您总是可以再次删除它。

Executing this code on my SQL Server instance gives me as a schedule 'Occurs every day every 1 hour(s) between 23:30:00 and 23:59:59', and I think that is not what you want. 在我的SQL Server实例上执行此代码可以作为时间表“在23:30:00和23:59:59之间每1小时每天发生一次”,我认为这不是您想要的。

Can anyone confirm that the following SQL code is right for my task 任何人都可以确认以下SQL代码适合我的任务

Answer: nope, it's not right. 答:不,这是不对的。

Like Ben Thul and David M are suggesting in their answers: you can just try the code and check the job to see if it does what you want, and it would be even better to create a new job by right-clicking on Jobs in the SQL Server Agent and choosing New Job... . 就像Ben Thul和David M在他们的答案中所建议的那样:您可以尝试代码并检查作业以查看其是否满足您的要求,并且通过右键单击Jobs中的Jobs来创建新作业会更好。 SQL Server Agent然后选择New Job...

You can generate script from sql job agent and run it its a one way. 您可以从sql作业代理生成脚本并以一种方式运行它。 The other way is to generate task using Window Task Schedule from the wizard. 另一种方法是使用向导中的“窗口任务计划”来生成任务。 for that go to my computer --> right click on it and select Manage option. 为此,请转到我的计算机->右键单击它,然后选择管理选项。 it will open server manager. 它将打开服务器管理器。 now go to configuration option and select Task Scheduler. 现在转到配置选项,然后选择任务计划程序。 you can just create task from create task option right side of the form in action tab. 您只需在操作标签的表单右侧,从创建任务选项创建任务即可。 its very easy way to generate job from here. 这是从这里产生工作的非常简单的方法。 Actually i also using same thing for my project requirement. 其实我也为我的项目需求使用相同的东西。

In that i just create task that will execute one store procedure every day at 12:00 once. 我只是创建一个任务,每天12:00执行一次存储过程。 So you can also do same way if not run that script and do all the stuff in script instead. 因此,如果不运行该脚本,也可以执行相同的方法,而是执行脚本中的所有内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM