背景
每年12月,为线上数据库添加旬表(一般为每月3张,格式为tablenameby202112_1,tablenameby202112_2,tablenameby202112_3) 可根据需要更改。(其中包括表结构,索引,用户权限等)
写在前面
本次使用的是游标,因为使用频率为1年添加1次,频率不高。读者可以根据需要建立存储过程并建立计划任务,使其自动执行。
例子:
create procedure [dbo].[Create_points_table] --存储过程里面放置游标
as
begin
您的游标内容(自定义哟)
end
每月3张
#其中根据需要更改起止时间,本次为2021年每月
declare @tablename varchar(50)
declare @sql01 varchar(1000)
declare @sql02 varchar(1000)
declare @sql03 varchar(1000)
declare Create_points_table cursor
for select convert(varchar(6),dateadd(mm,number,'2021-01-01'),112) --每月3张旬表(通过varchar(6)控制输出的字符数202001)
from master..spt_values with(nolock)
where type='P' and number>=0
and dateadd(mm,number,'2021-01-01')<'2022-01-01'
open Create_points_table
fetch next from Create_points_table into @tablename
while(@@fetch_status=0)
begin
set @sql01 = 'CREATE TABLE [dbo].[t_web_log_'+@tablename+'_1](
[id] bigint primary key not null,
[method_name] varchar(100) NULL ,
[request_url] varchar(255) NULL ,
[request_ip] varchar(50) NULL ,
[request_params] text NULL ,
[response_data] text NULL ,
[time_span] int NULL default 0 ,
[create_time] datetime NOT NULL ,
[result_success] bit NULL DEFAULT ((0)) ,
); CREATE INDEX [time_index] ON [dbo].[t_web_log_'+@tablename+'_1]
([create_time] DESC);'
set @sql02 = 'CREATE TABLE [dbo].[t_web_log_'+@tablename+'_2](
[id] bigint primary key not null,
[method_name] varchar(100) NULL ,
[request_url] varchar(255) NULL ,
[request_ip] varchar(50) NULL ,
[request_params] text NULL ,
[response_data] text NULL ,
[time_span] int NULL default 0 ,
[create_time] datetime NOT NULL ,
[result_success] bit NULL DEFAULT ((0)) ,
); CREATE INDEX [time_index] ON [dbo].[t_web_log_'+@tablename+'_2]
([create_time] DESC);'
set @sql03 = 'CREATE TABLE [dbo].[t_web_log_'+@tablename+'_3](
[id] bigint primary key not null,
[method_name] varchar(100) NULL ,
[request_url] varchar(255) NULL ,
[request_ip] varchar(50) NULL ,
[request_params] text NULL ,
[response_data] text NULL ,
[time_span] int NULL default 0 ,
[create_time] datetime NOT NULL ,
[result_success] bit NULL DEFAULT ((0)) ,
); CREATE INDEX [time_index] ON [dbo].[t_web_log_'+@tablename+'_3]
([create_time] DESC);'
execute(@sql01)
execute(@sql02)
execute(@sql03)
fetch next from Create_points_table into @tablename
end
close Create_points_table --关闭
deallocate Create_points_table --释放
结果
每月1张
declare @tablename varchar(50)
declare @sql varchar(1000)
declare Create_points_table cursor
for select convert(varchar(6),dateadd(mm,number,'2020-01-01'),112) --每月一账分表(通过varchar(6)控制输出的字符数202001)
from master..spt_values with(nolock)
where type='P' and number>=0
and dateadd(mm,number,'2020-01-01')<'2021-01-01'
open Create_points_table
fetch next from Create_points_table into @tablename
while(@@fetch_status=0)
begin
set @sql = 'CREATE TABLE [dbo].[t_web_log_'+@tablename+'](
[id] bigint primary key not null,
[method_name] varchar(100) NULL ,
[request_url] varchar(255) NULL ,
[request_ip] varchar(50) NULL ,
[request_params] text NULL ,
[response_data] text NULL ,
[time_span] int NULL default 0 ,
[create_time] datetime NOT NULL ,
[result_success] bit NULL DEFAULT ((0)) ,
); CREATE INDEX [time_index] ON [dbo].[t_web_log_'+@tablename+']
([create_time] DESC);'
execute(@sql)
fetch next from Create_points_table into @tablename
end
close Create_points_table --关闭
deallocate Create_points_table --释放
结果
每月3张的语句就是从每月一张的语句基础上更改的
每天1张
declare @tablename varchar(50)
declare @sql varchar(1000)
declare Create_points_table cursor
for select convert(varchar(50),dateadd(day,n.number,'2021-01-01'),112)
from master.dbo.spt_values n
where n.type = 'p'
and number < datediff(day,'2021-01-01','2022-01-01')
open Create_points_table
fetch next from Create_points_table into @tablename
while(@@fetch_status=0)
begin
set @sql = 'CREATE TABLE [dbo].[t_web_log_'+@tablename+'](
[id] bigint primary key not null,
[method_name] varchar(100) NULL ,
[request_url] varchar(255) NULL ,
[request_ip] varchar(50) NULL ,
[request_params] text NULL ,
[response_data] text NULL ,
[time_span] int NULL default 0 ,
[create_time] datetime NOT NULL ,
[result_success] bit NULL DEFAULT ((0)) ,
); CREATE INDEX [time_index] ON [dbo].[t_web_log_'+@tablename+']
([create_time] DESC);'
execute(@sql)
fetch next from Create_points_table into @tablename
end
close Create_points_table --关闭
deallocate Create_points_table --释放
结果
删除所有表
仅用于测试时,使用。在线上使用时,请注意游标中的过滤添加(选择性删除哟,name != 'sysdiagrams' )
declare @tablename varchar(50)
declare @sql varchar(1000)
declare cu_adcolumn cursor for select name from sys.tables where name != 'sysdiagrams'
open cu_adcolumn
fetch next from cu_adcolumn into @tablename
while(@@fetch_status=0)
begin
set @sql = 'drop table ['+@tablename+'] '
execute(@sql)
fetch next from cu_adcolumn into @tablename
end
close cu_adcolumn --关闭
deallocate cu_adcolumn --释放
添加索引
(
为以上添加的旬表添加索引
)
对表的右键(编写表脚本为,create到,新查询编辑器窗口)只会显示表结构
。
所以需要使用数据库
导出(生成脚本,高级选项中,选择索引和仅架构)导出为sql文件,然后使用上面的建表语句和下面的创建索引语句(需要分开执行)。
需要修改name LIKE 'Orderby2021%';和表名的变量ON ['+@tableName+'],索引的个数需要自定义
declare @tableName varchar(50)
declare @sql01 varchar(max)
declare @sql02 varchar(max)
declare mycur cursor for select name from dbo.sysobjects where type='U' AND name LIKE 'Orderby2021%';
OPEN mycur
fetch next from mycur into @tableName
while @@FETCH_STATUS=0
begin
set @sql01='CREATE NONCLUSTERED INDEX [time_index] ON ['+@tableName+']
(
[create_time] DESC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]';
set @sql02='CREATE NONCLUSTERED INDEX [time_span_index] ON ['+@tableName+']
(
[time_span] DESC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
';
-- print @sql01
-- print @sql02
exec (@sql01)
exec (@sql02)
fetch next from mycur into @tableName
end
close mycur
deallocate mycur
添加用户表权限
使用时请按需修改
declare @tableName varchar(50)
declare @sql01 varchar(max)
declare @sql02 varchar(max)
declare mycur cursor for select name from dbo.sysobjects where type='U' AND name LIKE 'Orderby2021%';
OPEN mycur
fetch next from mycur into @tableName
while @@FETCH_STATUS=0
begin
set @sql01='grant select,insert,delete,update on ['+ @tableName+'] to testuser01';
set @sql02='grant select,insert,delete,update on ['+ @tableName+'] to sa2';
-- print @sql01
-- print @sql02
exec (@sql01)
exec (@sql02)
fetch next from mycur into @tableName
end
close mycur
deallocate mycur
go
base表
(读者需要根据作业逻辑判断自己的环境) base表添加数据(数据为分表的后缀)(作用为引导外部数据写入分表,所以很重要),顺序为添加完对应旬表之后再添加
declare @tablename varchar(50)
declare @sql01 varchar(1000)
declare @sql02 varchar(1000)
declare @sql03 varchar(1000)
declare Create_points_table cursor
for select convert(varchar(6),dateadd(mm,number,'2021-01-01'),112) --每月一账分表(通过varchar(6)控制输出的字符数202001)
from master..spt_values with(nolock)
where type='P' and number>=0
and dateadd(mm,number,'2021-01-01')<'2022-01-01'
open Create_points_table
fetch next from Create_points_table into @tablename
while(@@fetch_status=0)
begin
set @sql01 ='INSERT INTO [dbo].[base] (id) VALUES (''by'+@tablename+'_1'');'
set @sql02 ='INSERT INTO [dbo].[base] (id) VALUES (''by'+@tablename+'_2'');'
set @sql03 ='INSERT INTO [dbo].[base] (id) VALUES (''by'+@tablename+'_3'');'
execute(@sql01)
execute(@sql02)
execute(@sql03)
fetch next from Create_points_table into @tablename
end
close Create_points_table --关闭
deallocate Create_points_table --释放
小结
时间函数+系统库表进行语句拼接。其中master…spt_values with(nolock)
where type='P’限制为最大的数为2047
select * from master…spt_values where type=‘P’ order by number desc
也就是说,添加旬表的最大时间不能大约2047年。(也就是27年后以上脚本就不能执行成功了)
之后为项目04(Mysql添加旬表02),使用游标还可以批量添加字段(写在其他篇幅)
本文说明,主要技术内容来自互联网技术大佬的分享,还有一些自我的加工(仅仅起到注释说明的作用)。如有相关疑问,请留言,将确认之后,执行侵权必删