1、批量创建表,使用存在的[Test]表创建新表

declare @hh int
declare @hhstr varchar(2)
declare @tableName varchar(30)
set @hh=6
while (@hh<24)
 begin
 select @hhstr= right('00'+convert(varchar(2),@hh),2)
 set @tableName='v_20210113'+@hhstr
 execute('select * into '+@tableName+' from [mytest].[dbo].[Test]')
 set @hh=@hh+1
end 

2、批量删除表

declare @hh int
declare @hhstr varchar(2)
declare @tableName varchar(30)
set @hh=6
while (@hh<24)
 begin
 select @hhstr= right('00'+convert(varchar(2),@hh),2)
 set @tableName='v_20210113'+@hhstr
 execute('drop table '+@tableName)
 set @hh=@hh+1
end 

备注:

select @hhstr= right('00'+convert(varchar(2),@hh),2)

如果@hh=2,则@hhstr=‘02’,用于格式化数字成字符串

 

相关文章:

  • 2021-09-23
  • 2021-06-01
  • 2021-11-30
  • 2021-11-30
  • 2021-11-30
  • 2021-11-30
  • 2021-12-03
  • 2021-07-13
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-11
相关资源
相似解决方案