Job内容大概如下,给你伪代码.declare @sql varchar(6000)-- sql每个月生成一张表,表名相似按年月命名 set @sql='create table tab'+convert(varchar(6),getdate(),112)+' ([表字段列表])'exec(@sql) -- 设置自动删掉三个月以外的表 while(exists(select 1 from sysobjects where xtype='U' and name like 'tab%' and datediff(m,right(name,6)+'01',getdate())>=3)) begin select @sql='drop table ['+name+'] ' from sysobjects where xtype='U' and name like 'tab%' and datediff(m,right(name,6)+'01',getdate())>=3
--管理
--SQL Server代理
--右键作业
--新建作业
--"常规"项中输入作业名称
--"步骤"项
--新建
--"步骤名"中输入步骤名
--"类型"中选择"Transact-SQL 脚本(TSQL)"
--"数据库"选择执行命令的数据库
--"命令"中输入要执行的语句:
EXEC 存储过程名 ... --该存储过程用于创建表 --确定
--"调度"项
--新建调度
--"名称"中输入调度名称
--"调度类型"中选择你的作业执行安排
--如果选择"反复出现"
--点"更改"来设置你的时间安排
然后将SQL Agent服务启动,并设置为自动启动,否则你的作业不会被执行 设置方法:
我的电脑--控制面板--管理工具--服务--右键 SQLSERVERAGENT--属性--启动类型--选择"自动启动"--确定.
主要是怎样提取表名进行删除?
在存储过程中,动态拼接表名,然后动态执行之.例如:
declare @mm as varchar(6)
set @mm = convert(varchar(6),dateadd(mmm,-3,getdate()),112)
declare @tb as varchar(50)
set @tb = 'tb_' + @mm
exec('drop table ' + @tb)
set @sql='create table tab'+convert(varchar(6),getdate(),112)+' ([表字段列表])'exec(@sql)
-- 设置自动删掉三个月以外的表
while(exists(select 1 from sysobjects
where xtype='U' and name like 'tab%'
and datediff(m,right(name,6)+'01',getdate())>=3))
begin
select @sql='drop table ['+name+'] '
from sysobjects
where xtype='U' and name like 'tab%'
and datediff(m,right(name,6)+'01',getdate())>=3
exec(@sql)
end