Project 04 (Sql server add ten-day table 01)

background

每年12月,为线上数据库添加旬表(一般为每月3张,格式为tablenameby202112_1,tablenameby202112_2,tablenameby202112_3) 可根据需要更改。(其中包括表结构,索引,用户权限等)

Write in front
本次使用的是游标,因为使用频率为1年添加1次,频率不高。读者可以根据需要建立存储过程并建立计划任务,使其自动执行。

例子:
create procedure [dbo].[Create_points_table]    --存储过程里面放置游标
as
begin
您的游标内容(自定义哟)
end

3 sheets per month

#其中根据需要更改起止时间,本次为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  --释放

result
Insert picture description here

1 per month

	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  --释放

result
Insert picture description here
每月3张的语句就是从每月一张的语句基础上更改的

1 per day

	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  --释放

result
Insert picture description here

Delete all tables

仅用于测试时,使用。在线上使用时,请注意游标中的过滤添加(选择性删除哟,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  --释放

Add index

( 为以上添加的旬表添加索引)
Right click on the table (for writing table script, create to, new query editor window) 只会显示表结构.
So you need to use 数据库export (generate script, advanced options, select index and only schema) to export as a sql file, and then use the above table creation statement and the following index creation statement (need to be executed separately).

需要修改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

Add user table permissions

使用时请按需修改
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 table

(读者需要根据作业逻辑判断自己的环境) 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  --释放

summary

Time function + system library table for sentence splicing. Among them, master...spt_values ​​with(nolock)
where type='P' is limited to the maximum number of 2047
select * from master...spt_values ​​where type='P' order by number desc In
other words, the maximum time to add a ten-year table cannot be about 2047 year. (That is, the script above cannot be executed successfully after 27 years)

之后为项目04(Mysql添加旬表02),使用游标还可以批量添加字段(写在其他篇幅)

This article explains that the main technical content comes from the sharing of Internet technology giants, as well as some self-processing (only for the role of annotations). If related questions, please leave a message after the confirmation, the implementation of infringement will be deleted

Guess you like

Origin blog.csdn.net/baidu_34007305/article/details/110873568