Create a generic stored procedure for a job

Source: Internet
Author: User
Create | Stored procedures
if exists (SELECT * from dbo.sysobjects WHERE id = object_id (N ' [dbo].[ P_jobset] and OBJECTPROPERTY (ID, N ' isprocedure ') = 1) drop procedure [dbo]. [P_jobset] Go

/*--timed call to stored procedures

Creates an automatically deleted job job that invokes the specified stored procedure at a specified time when the execution completes

--Jiangjian 2004.07--*/

/*--Call Example

exec p_jobset ' Master.dbo.xp_cmdshell ', ' 2004-1-1 10:30 '--*/create proc, p_jobset@prorcname sysname,--to invoke the stored procedure name of a timed call, If it is not in the current library, use: library name. Owner name. stored Procedure name @job_date datetime--The execution time of the stored procedure (including time information) asdeclare @dbname sysname, @jobname sysname, @date int, @time int

Select @jobname = ' timed send job _ ' +cast (newid () as varchar), @date =convert (varchar, @job_date, 112), @time =replace (convert (varchar, @job_date, 108), ': ', '

if exists (select 1 from msdb.. sysjobs where name= @jobname) exec msdb. Sp_delete_job @job_name = @jobname

--Create job exec msdb ... Sp_add_job @job_name = @jobname, @delete_level =1

--Create job step declare @sql varchar Select @sql = ' exec ' + @prorcname, @dbname =db_name ()

EXEC msdb.. Sp_add_jobstep @job_name = @jobname, @step_name = ' send processing steps ', @subsystem = ' TSQL ', @database_name = @dbname, @command = @sql, @r etry_attempts = 5--Number of retries @retry_interval = 5--Retry interval

--Create a dispatch exec msdb. sp_add_jobschedule @job_name = @jobname, @name = ' time schedule ', @enabled = 1, @freq_type = 1, @active_start_date = @date, @act Ive_start_time = @time

--Add target server exec msdb.dbo.sp_add_jobserver @job_name = @jobname, @server_name = N ' (local) ' Go


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.