数据备份后 清空表
请教大侠们,我想在sql数据库的一个表a 定期进行备份,备份成功后就自动清空表,因为表隔一段时间就太大了,需要定期备份后清空一下。
我是新手,多谢了。
------解决方案--------------------建立job
------解决方案--------------------你可以建立一个作业自动备份数据库,备份成功后可以通过存储过程实现
------解决方案---------------------- 创建自动备份数据库的作业
USE msdb
GO
BEGIN TRANSACTION
DECLARE @Database VarChar(20), @BackupDatabase VarChar(30), @description VarChar(50), @step_name VarChar(50)
DECLARE @command VarChar(250), @ScheduledBackupName VarChar(50), @RAISERRORMESS VarChar(250), @ActiveStartTime Integer
DECLARE @JobID BINARY(16),@ReturnCode INT
SET @Database = N '你的数据库名称 '-- 数据库名称
SET @BackupDatabase = N 'Backup ' + @Database
SET @RAISERRORMESS = N '无法导入作业“ ' + @BackupDatabase + '”,因为已经有相同名称的多重服务器作业。 '
SET @description = N '备份 ' + @Database + '数据库 '
SET @step_name = N 'Job ' + @BackupDatabase
SET @command = N 'EXEC pro_AutoBackup ' ' ' + @Database + ' ' ' '
SET @ScheduledBackupName = N 'Scheduled ' + @BackupDatabase
SET @ActiveStartTime = 142700-- 每天的备份时间
SET @ReturnCode = 0
IF (SELECT COUNT(*) FROM msdb.dbo.syscategories WHERE name = N 'Database Maintenance ') < 1
EXECUTE msdb.dbo.sp_add_category @name = N 'Database Maintenance '
-- 删除同名的警报(如果有的话)。
SELECT @JobID = job_id FROM msdb.dbo.sysjobs WHERE (name = @BackupDatabase)
IF (@JobID IS NOT NULL)
BEGIN
-- 检查此作业是否为多重服务器作业
IF (EXISTS (SELECT * FROM msdb.dbo.sysjobservers WHERE (job_id = @JobID) AND (server_id <> 0)))
BEGIN
-- 已经存在,因而终止脚本
RAISERROR (@RAISERRORMESS, 16, 1)
GOTO QuitWithRollback
END
ELSE
-- 删除[本地]作业
EXECUTE msdb.dbo.sp_delete_job @job_name = @BackupDatabase
SELECT @JobID = NULL
END
BEGIN
-- 添加作业
EXECUTE @ReturnCode = msdb.dbo.sp_add_job @job_id = @JobID OUTPUT , @job_name = @BackupDatabase, @owner_login_name = N 'sa ',
@description = @description, @category_name = N 'Database Maintenance ', @enabled = 1, @notify_level_email = 0,
@notify_level_page = 0, @notify_level_netsend = 0, @notify_level_eventlog = 2, @delete_level= 0
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
-- 添加作业步骤
EXECUTE @ReturnCode = msdb.dbo.sp_add_jobstep @job_id = @JobID, @step_id = 1, @step_name = @step_name, @command = @command,
@database_name = @Database, @server = N ' ', @database_user_name = N ' ', @subsystem = N 'TSQL ', @cmdexec_success_code = 0,
@flags = 0, @retry_attempts = 0, @retry_interval = 1, @output_file_name = N ' ', @on_success_step_id = 0,
@on_success_action = 1, @on_fail_step_id = 0, @on_fail_action = 2
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXECUTE @ReturnCode = msdb.dbo.sp_update_job @job_id = @JobID, @start_step_id = 1
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
-- 添加作业调度
EXECUTE @ReturnCode = msdb.dbo.sp_add_jobschedule @job_id = @JobID, @name = @ScheduledBackupName, @enabled = 1,
@freq_type = 4, @active_start_date = 20061028, @active_start_time = @ActiveStartTime, @freq_interval = 1,
@freq_subday_type = 1, @freq_subday_interval = 0, @freq_relative_interval = 0, @freq_recurrence_factor = 0,
@active_end_date = 99991231, @active_end_time = 235959
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
-- 添加目标服务器
EXECUTE @ReturnCode = msdb.dbo.sp_add_jobserver @job_id = @JobID, @server_name = N '(local) '
IF (@@ERROR <> 0 OR @