SQL commands for regularly deleting historical data from databases and SQL commands for historical data

Source: Internet
Author: User
Tags management studio sql server management sql server management studio

SQL commands for regularly deleting historical data from databases and SQL commands for historical data


Create a job,Used to regularly clear historical database data

Delete the data of the first quarter of the table before gps_comm, as follows:

Elete from gps_comm

WHERE (date <GETDATE ()-120)

If you want to continue to delete the data from the first 120 days in another table, you can continue to add the data, for example:

Delete from PzTable

WHERE (PZTIME <GETDATE ()-120)


How to regularly delete a SQL database

USE master
Go

DECLARE @ s NVARCHAR (4000)
SET @ s =''
SELECT @ s = @ s + ''+ 'drop database' + QUOTENAME (name) FROM master .. sysdatabases WHERE dbID> 4

EXEC (@ s)
Go

Job operations

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:
The script provided above

-- 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.

Scheduled Database Deletion

With SQL Server jobs, that is, jobs, you can execute the specified stored procedure on a regular basis and write SQL statements in the stored procedure. Just do what you want. As for the code, the job code is not directly written, and is implemented through interface operations. SQL server 2000 is under the SQL Server Agent under the Enterprise Manager, and SQL server 2005/2008 is under the SQL Server Agent under SQL Server Management Studio. You can search for details.

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.