日期:2014-05-17 浏览次数:20672 次
-- begin
use [数据库名]
declare @tabname varchar(50),@tsql varchar(100)
declare ap scroll cursor for
select 'YC'+convert(varchar,ds,112) 'tabname' from
(select dateadd(d,number,
stuff(
convert(varchar,dateadd(d,-1,
stuff(convert(varchar,getdate(),111)
,9,2,'01')),111)
,9,2,'01')) 'ds'
from master.dbo.spt_values
where type='P' and number<=32
) t
where datediff(m,t.ds,getdate())=1
open ap
fetch first from ap into @tabname
while(@@fetch_status<>-1)
begin
if exists(select 1 from sysobjects where xtype='U' and name=@tabname)
begin
select @tsql='drop table '+@tabname
exec(@tsql)
end
fetch next from ap into @tabname
end
close ap
deallocate ap
-- end
-- 注: 获取上月所有表名
select 'YC'+convert(varchar,ds,112) 'tabname' from
(select dateadd(d,number,
stuff(
convert(varchar,dateadd(d,-1,
stuff(convert(varchar,getdate(),111)
,9,2,'01')),111)
,9,2,'01')) 'ds'
from master.dbo.spt_values
where type='P' and number<=32
) t
where datediff(m,t.ds,getdate())=1
/*
tabname
--------------------------------
YC20130901
YC20130902
YC20130903
YC20130904
YC20130905
YC2013090