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.