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
exec(@sql)
end
------解决方案--------------------