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
DECLARE @ s NVARCHAR (4000)
SET @ s =''
SELECT @ s = @ s + ''+ 'drop database' + QUOTENAME (name) FROM master .. sysdatabases WHERE dbID> 4
EXEC (@ s)
-- SQL Server proxy
-- Right-click a job
-- Create a job
-- Enter the job name in "general"
-- 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
-- "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.
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.