Files
chill_notes/OneNote/数据库知识库/工具脚本/循环批量删除数据库表.md
2026-04-15 23:43:17 +08:00

780 B
Executable File

--创建临时表
create table #Man(
id int identity(1,1),
tbName nvarchar(50)
) --往临时表插入数据
insert into #Man
select name from sys.tables
where create_date<='2018-03-10 15:36:35.687' and name not in ('DeviceNetState','EphInfo','FilterStatistic','UserInfo','AlmInfo')
order by create_date desc

--确认
select * from #Man

commit
rollback
begin tran

--定义循环变量
declare @i int
set @i = 1
declare @count int
select @count = count(*) from #Man

--print @count

declare @str nvarchar(500)
declare @tb nvarchar(50)

while(@i <= @count) begin

select @tb = isnull(tbName, '') from #Man where id = @i
--循环执行语句
set @str = 'drop table [' + @tb+ ']'
exec(@str)

set @str = ''
set @i = @i + 1
end