自动创建分区函数 按月分区
来源:这里教程网
时间:2026-03-02 11:07:29
作者:
declare @tableName varchar(50), @fileGroupName varchar(50), @ndfName varchar(50), @newNameStr varchar(50), @fullPath
varchar(50), @newDay varchar(50), @oldDay datetime, @partFunName varchar(50), @schemeName varchar(50),
@sqlstr varchar(1000)
set @tableName='DYDB'
set @newDay=CONVERT(varchar(10),DATEADD(mm, DATEDIFF(mm,0,getdate()), 0), 23 )--CONVERT(varchar(100), GETDATE(), 23)--23:按天 114:按时间
set @oldDay=cast(CONVERT(varchar(10),DATEADD(mm, DATEDIFF(mm,0,getdate())-1, 0), 112 ) as datetime)
set @newNameStr=left(Replace(Replace(@newDay,':','_'),'-','_'),7)
set @fileGroupName=N'G'+@newNameStr
set @ndfName=N'F'+@newNameStr+''
set @fullPath=N'E:\\SQLDataBase\\UserData\\'+@ndfName+'.ndf'
set @partFunName=N'pf_Time'
set @schemeName=N'ps_Time'
print @fullPath
print @fileGroupName
print @ndfName
--创建文件组
if exists(select * from sys.filegroups where name=@fileGroupName)
begin
print '文件组存在,不需添加'
end
else
begin
--exec('ALTER DATABASE '+@tableName+' ADD FILEGROUP ['+@fileGroupName+']')
print 'exec '+('ALTER DATABASE '+@tableName+' ADD FILEGROUP ['+@fileGroupName+']')
print '新增文件组'
if exists(select * from sys.partition_schemes where name =@schemeName)
begin
--exec('alter partition scheme '+@schemeName+' next used ['+@fileGroupName+']')
print 'exec '+('alter partition scheme '+@schemeName+' next used ['+@fileGroupName+']')
print '修改分区方案'
end
print 'exec '+('alter partition scheme '+@schemeName+' next used ['+@fileGroupName+']')
print '修改分区方案'
if exists(select * from sys.partition_range_values where function_id=(select function_id from
sys.partition_functions where name =@partFunName) and value=@oldDay)
begin
--exec('alter partition function '+@partFunName+'() split range('''+@newDay+''')')
print 'exec '+('alter partition function '+@partFunName+'() split range('''+@newDay+''')')
print '修改分区函数'
end
end
--创建NDF文件
if exists(select * from sys.database_files where [state]=0 and (name=@ndfName or physical_name=@fullPath))
begin
print 'ndf文件存在,不需添加'
end
else
begin
--exec('ALTER DATABASE '+@tableName+'ADD FILE (NAME ='+@ndfName+',FILENAME = '''+@fullPath+''')TO FILEGROUP ['+@fileGroupName+']')
print 'ALTER DATABASE '+@tableName+' ADD FILE (NAME ='+@ndfName+',FILENAME = '''+@fullPath+''')TO FILEGROUP ['+@fileGroupName+']'
print '新创建ndf文件'
end
--/*--------------------以上创建数据库的文件组和物理文件------------------------*/
--分区函数
if exists(select * from sys.partition_functions where name =@partFunName)
begin
print '此处修改需要在修改分区函数之前执行'
end
else
begin
--exec('CREATE PARTITION FUNCTION '+@partFunName+'(DateTime)AS RANGE RIGHT FOR VALUES ('''+@newDay+''')')
print 'CREATE PARTITION FUNCTION '+@partFunName+'(DateTime)AS RANGE RIGHT FOR VALUES ('''+@newDay+''')'
print '新创建分区函数'
end
--分区方案
if exists(select * from sys.partition_schemes where name =@schemeName)
begin
print '此处修改需要在修改分区方案之前执行'
end
else
begin
--exec('CREATE PARTITION SCHEME '+@schemeName+' AS PARTITION '+@partFunName+' TO (''PRIMARY'','''+@fileGroupName+''')')
print ('CREATE PARTITION SCHEME '+@schemeName+' AS PARTITION '+@partFunName+' TO (''PRIMARY'','''+@fileGroupName+''')')
print '新创建分区方案'
end
--print '---------------以下是变量定义值显示---------------------'
--print '当前数据库:'+@tableName
--print '当前日期:'+@newDay+'(用作随机生成的各种名称和分区界限)'
--print '合法命名方式:'+@newNameStr
--print '文件组名称:'+@fileGroupName
--print 'ndf物理文件名称:'+@ndfName
--print '物理文件完整路径:'+@fullPath
--print '分区函数:'+@partFunName
--print '分区方案:'+@schemeName
--/*
写成SP
--select @@servername
alter procedure sp_maintain_partion_fg (
@tableName varchar(50),
@inputdate datetime
)
as begin
declare
@fileGroupName varchar(50),
@ndfName varchar(50),
@newNameStr varchar(50),
@fullPath varchar(50),
@newDay varchar(50),
@oldDay datetime,
@partFunName varchar(50),
@schemeName varchar(50),
@sqlstr varchar(1000)
--set @tableName='DYDB'
set @newDay=CONVERT(varchar(10),DATEADD(mm, DATEDIFF(mm,0,@inputdate), 0), 23 )--CONVERT(varchar(100), @inputdate, 23)--23:按天 114:按时间
set @oldDay=cast(CONVERT(varchar(10),DATEADD(mm, DATEDIFF(mm,0,@inputdate)-1, 0), 112 ) as datetime)
set @newNameStr=left(Replace(Replace(@newDay,':','_'),'-','_'),7)
set @fileGroupName=N'G'+@newNameStr
set @ndfName=N'F'+@newNameStr+''
set @fullPath=N'E:\\SQLDataBase\\UserData\\'+@ndfName+'.ndf'
set @partFunName=N'pf_Time'
set @schemeName=N'ps_Time'
print @fullPath
print @fileGroupName
print @ndfName
--创建文件组
if exists(select * from sys.filegroups where name=@fileGroupName)
begin
print '文件组存在,不需添加'
end
else
begin
--exec('ALTER DATABASE '+@tableName+' ADD FILEGROUP ['+@fileGroupName+']')
print 'exec '+('ALTER DATABASE '+@tableName+' ADD FILEGROUP ['+@fileGroupName+']')
print '新增文件组'
if exists(select * from sys.partition_schemes where name =@schemeName)
begin
--exec('alter partition scheme '+@schemeName+' next used ['+@fileGroupName+']')
print 'exec '+('alter partition scheme '+@schemeName+' next used ['+@fileGroupName+']')
print '修改分区方案'
end
print 'exec '+('alter partition scheme '+@schemeName+' next used ['+@fileGroupName+']')
print '修改分区方案'
if exists(select * from sys.partition_range_values where function_id=(select function_id from
sys.partition_functions where name =@partFunName) and value=@oldDay)
begin
--exec('alter partition function '+@partFunName+'() split range('''+@newDay+''')')
print 'exec '+('alter partition function '+@partFunName+'() split range('''+@newDay+''')')
print '修改分区函数'
end
end
--创建NDF文件
if exists(select * from sys.database_files where [state]=0 and (name=@ndfName or physical_name=@fullPath))
begin
print 'ndf文件存在,不需添加'
end
else
begin
--exec('ALTER DATABASE '+@tableName+'ADD FILE (NAME ='+@ndfName+',FILENAME = '''+@fullPath+''')TO FILEGROUP ['+@fileGroupName+']')
print 'ALTER DATABASE '+@tableName+' ADD FILE (NAME ='+@ndfName+',FILENAME = '''+@fullPath+''')TO FILEGROUP ['+@fileGroupName+']'
print '新创建ndf文件'
end
--/*--------------------以上创建数据库的文件组和物理文件------------------------*/
end
----分区函数
--if exists(select * from sys.partition_functions where name =@partFunName)
--begin
--print '此处修改需要在修改分区函数之前执行'
--end
--else
--begin
----exec('CREATE PARTITION FUNCTION '+@partFunName+'(DateTime)AS RANGE RIGHT FOR VALUES ('''+@newDay+''')')
--print 'CREATE PARTITION FUNCTION '+@partFunName+'(DateTime)AS RANGE RIGHT FOR VALUES ('''+@newDay+''')'
--print '新创建分区函数'
--end
----分区方案
--if exists(select * from sys.partition_schemes where name =@schemeName)
--begin
--print '此处修改需要在修改分区方案之前执行'
--end
--else
--begin
----exec('CREATE PARTITION SCHEME '+@schemeName+' AS PARTITION '+@partFunName+' TO (''PRIMARY'','''+@fileGroupName+''')')
--print ('CREATE PARTITION SCHEME '+@schemeName+' AS PARTITION '+@partFunName+' TO (''PRIMARY'','''+@fileGroupName+''')')
--print '新创建分区方案'
--end
--exec sp_maintain_partion_fg 'XXXX','2013-03-20'
编辑推荐:
- 查看用户连接到特定数据库03-02
- 自动创建分区函数 按月分区03-02
- enable cdc capture on mssql db03-02
- create partition sheme, partition function03-02
- sqlserver赋予用户有查看对象定义的语句03-02
- 分区实战03-02
- SQLSERVER数据库状态揭秘03-02
- mssql 创建dblink03-02
下一篇:
相关推荐
-
雷神推出 MIX PRO II 迷你主机:基于 Ultra 200H,玻璃上盖 + ARGB 灯效
2 月 9 日消息,雷神 (THUNDEROBOT) 现已宣布推出基于英
-
制造商 Musnap 推出彩色墨水屏电纸书 Ocean C:支持手写笔、第三方安卓应用
2 月 10 日消息,制造商 Musnap 现已在海外推出一款 Oce
热文推荐
- SQLSERVER数据库状态揭秘
SQLSERVER数据库状态揭秘
26-03-02 - SQL SERVER运维日记--备份
SQL SERVER运维日记--备份
26-03-02 - Ubuntu高可用监控部署指南(手把手教你配置Keepalived实现Linux服务器高可用性)
- 屏蔽机房壳体龙骨安装焊接教程
屏蔽机房壳体龙骨安装焊接教程
26-03-02 - NTP时间服务器,为安防监控系统保驾护航
NTP时间服务器,为安防监控系统保驾护航
26-03-02 - 高清视频监控系统基本组成
高清视频监控系统基本组成
26-03-02 - 深入理解 SQL Server 2008 的锁机制
深入理解 SQL Server 2008 的锁机制
26-03-02 - 新建可用性组的正将TEST连接到XXXX上的可用性组TAG01报错和解决--alwayson搭建踩的坑
- SQL SERVER数据库收缩最佳实践
SQL SERVER数据库收缩最佳实践
26-03-02 - SQLServer执行存储过程失败(sys.xp_cmdshell)问题处理
