Create a job executed on the last workday of each month and call the above stored procedure to automatically create a database

Source: Internet
Author: User

-- Create a job executed on the last workday of each month and call the above stored procedure to automatically create a database

Use master
Go

-- Set the SQL Agent service to automatically start
Exec MSDB .. sp_set_sqlagent_properties @ auto_start = 1
Go

-- Create a job
Exec MSDB .. sp_add_job @ job_name = n' automatic database creation Process'

-- Create a job
Declare @ SQL varchar (800), @ dbname varchar (250)
Select @ SQL = n' exec sp_proccopydb' -- call the stored procedure of the automatic database creation
, @ Dbname = db_name () -- Name of the database that executes automatic database creation

Exec MSDB .. sp_add_jobstep @ job_name = n 'automatic database creation process ',
@ Step_name = 'database creation process ',
@ Subsystem = 'tsql ',
@ Database_name = @ dbname,
@ Command = @ SQL,
@ Retry_attempts = 5, -- number of retries
@ Retry_interval = 5 -- Retry Interval

-- Add Job Scheduling (the last workday of each month)
Exec MSDB. DBO. sp_add_jobschedule @ job_name = n' automatic database creation process ',
@ Name = n' schedule ',
@ Freq_type = 32,
@ Active_start_time = 0, -- start execution at 0
@ Freq_interval = 9, -- last workday of each month
@ Freq_subday_type = 1,
@ Freq_subday_interval = 0,
@ Freq_relative_interval = 16,
@ Freq_recurrence_factor = 1

-- Add the target server
Exec MSDB. DBO. sp_add_jobserver
@ Job_name = n' automatic database creation process ',
@ SERVER_NAME = n' (local )'

/* -- Known issues

Due to the SQL script generation bug, some databases may not be able to use the script generation method for processing.

--*/

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.