作业计划到SQL作业

时间:2015-12-18 23:53:19

标签: sql-server tsql

如何使用sp_add_job安排作业每5分钟无限期运行一次? 我应该使用@ freq_type = 4,@ freq_interval = 64,freq_subday_type = 0x4,@ freq_subday_interval = 10,@ freq_relative_interval = 0的确切值是什么?

1 个答案:

答案 0 :(得分:0)

试试这个.... @ user2748147

Declare @job nvarchar(128),
@mycommand nvarchar(max), 
@servername nvarchar(28),
@databasename nvarchar(50),
@startdate nvarchar(8),
@starttime nvarchar(8)

SET @job = 'myJob'
SET @mycommand = 'Select * from myTable'
SET @startdate = '20151219' -- The date December 19, 2015
SET @starttime = '160000' -- The time, 16:00:00
SET @servername = 'myservername'
SET @databasename = 'yourdatabasename'

--Add a job
EXEC dbo.sp_add_job
    @job_name = @job ;
--Add a job step named process step. This step runs the stored procedure
EXEC sp_add_jobstep
    @job_name = @job,
    @step_name = N'process step',
    @subsystem = N'TSQL',
    @database_name = @databasename,
    @command = @mycommand
--Schedule the job at a specified date and time
exec sp_add_jobschedule @job_name = @job,
@name = 'MySchedule',
@freq_type=4,
@freq_interval=4,
@freq_subday_type=0x4,
@freq_subday_interval = 5,
@active_start_date = @startdate,
@active_start_time = @starttime
-- Add the job to the SQL Server Server
EXEC dbo.sp_add_jobserver
    @job_name =  @job,
    @server_name = @servername