SQL Server periodically executes SQL statements

Source: Internet
Author: User

First:

Formulation of scheduled jobs

Enterprise Manager

-- Manage

-- SQL Server proxy

-- Right-click a job

-- Create a job

-- Enter the job name in "general"

-- "Step"

-- New

-- Enter the step name in "Step name"

-- Select "Transact-SQL script (tsql)" in "type )"

-- "Database": select the database for Command Execution

-- Enter the statement to be executed in "command:

Exec stored procedure name... -- this stored procedure is used to create a table

-- OK

-- "Scheduling" item

-- Create Scheduling

-- Enter the scheduling name in "name"

-- Select your job execution schedule in "scheduling type"

-- If "repeated appears" is selected"

-- Click "change" to set your schedule

Then start the SQL Agent service and set it to Automatic startup. Otherwise, your job will not be executed.

Setting method:

My computer -- control panel -- Administrative Tools -- service -- Right-click SQLServerAgent -- properties -- start type -- select "auto start" -- OK.

Note:

1. select "database maintenance" for the category in the properties of the new job. 2. during scheduling, You need to select the specific time point for daily execution to ensure that the SQL commands for the steps are executed at the specified time.

SQL Server periodically executes SQL statements

Stored Procedure

Set ansi_nulls on
Set quoted_identifier on
Go

Alter procedure [DBO]. [automodify]
As
Declare @ ID int
Declare @ order_status int
Declare @ fhtime datetime
Declare @ add_date datetime
Declare pcurr cursor
Select ID, order_status, dateadd (day, 10, fhtime) as fhtime,
Dateadd (day, 14, add_date) as add_date from DBO. orderinfo where order_status = 1 or order_status = 3
Open pcurr
Fetch next from pcurr into @ ID, @ order_status, @ fhtime, @ add_date
While (@ fetch_status = 0)
Begin
If (@ order_status = 3)
Begin
If (@ fhtime <getdate ())
Begin
-- Print 'A'
Update DBO. orderinfo set order_status = 4 where [ID] = @ ID
End
End
Else if (@ order_status = 1)
Begin
If (@ add_date <getdate ())
Begin
-- Print 'B'
Update DBO. orderinfo set order_status = 0 where [ID] = @ ID
End
End
Fetch next from pcurr into @ ID, @ order_status, @ fhtime, @ add_date
End
Close pcurr
Deallocate pcurr

 

Exec [automodify]

Related Article

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.