SqlServer 循环建表、删除表、更新表

常用于分库分表

1、批量删除

declare @outter int
declare @inner int
declare @tablePrefix varchar(30)
declare @tableName varchar(36)
set @outter=1
set @inner=0
while (@outter<=12)
begin
 set @tablePrefix='BankPayOrder_'+convert(varchar,@outter)+'_'
 while(@inner<=9)
 begin 
 set @tableName=@tablePrefix+convert(varchar,@inner)
 execute('delete from '+@tableName+'')
 set @inner=@inner+1
 end
 set @inner=0
 set @outter=@outter+1
end 

2、批量建表

declare @outter int
declare @inner int
declare @tablePrefix varchar(30)
declare @tableName varchar(36)
set @outter=1
set @inner=0
while (@outter<=12)
begin
 set @tablePrefix='BankPayOrder_'+convert(varchar,@outter)+'_'
 while(@inner<=9)
 begin 
 set @tableName=@tablePrefix+convert(varchar,@inner)
 execute('select * into '+@tableName+' from [BankPayOrderCommission_2018].[dbo].[BankPayOrder_1_0]')
 set @inner=@inner+1
 end
 set @inner=0
 set @outter=@outter+1
end 

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券