SQL Server分区脚本备份
Posted saratearing
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL Server分区脚本备份相关的知识,希望对你有一定的参考价值。
--生成分区脚本
DECLARE @DataBaseName NVARCHAR(50)--数据库名称
DECLARE @TableName NVARCHAR(50)--表名称
DECLARE @ColumnName NVARCHAR(50)--字段名称
DECLARE @Location NVARCHAR(50)--保存分区文件的路径
DECLARE @PStartDay DATE--分区开始时间点
DECLARE @PEndDay DATE --分区截止时间点
DECLARE @FGStr NVARCHAR(50) --文件组名
DECLARE @FStr NVARCHAR(50) --文件名
DECLARE @Size NVARCHAR(50)=‘10MB‘--分区初始化大小,默认为10MB
DECLARE @FileGrowth NVARCHAR(50)=‘100MB‘--分区文件增量 ,默认值为100MB
DECLARE @sql NVARCHAR(max)
--设置下面变量
SET @DataBaseName = ‘BigDBTest‘
SET @TableName = ‘Dim_Date‘
SET @ColumnName = ‘DateKey‘
SET @Location = ‘H:\partion\‘
SET @PStartDay=‘2015-01-01‘
SET @PEndDay=‘2016-02-01‘
SET @FGStr = ‘‘
SET @FStr = ‘‘
DECLARE @Y INT
--1.创建文件组
PRINT ‘--1.创建文件组‘
SET @Y = YEAR(@PStartDay)
WHILE @Y <= YEAR(@PEndDay)
BEGIN
SET @FGStr = @Y
SET @sql = ‘ALTER DATABASE [‘[email protected] +‘] ADD FILEGROUP [FG_‘[email protected]+‘]‘
PRINT @sql + CHAR(13)
SET @Y = @Y+1
END
--2.创建文件
PRINT CHAR(13)+‘--2.创建文件‘
SET @Y = YEAR(@PStartDay)
WHILE @Y <=YEAR(@PEndDay)
BEGIN
SET @[email protected]
SET @[email protected]
SET @sql = ‘ALTER DATABASE [‘[email protected] +‘]
ADD FILE
(NAME = N‘‘FG_‘[email protected]+‘_data‘‘,FILENAME = N‘‘‘[email protected]+‘FG_‘[email protected]+‘_data.ndf‘‘,SIZE = ‘[email protected]+‘, FILEGROWTH = ‘[email protected]+‘ )
TO FILEGROUP [FG_‘[email protected]+‘];‘
PRINT @sql + CHAR(13)
SET @Y = @Y+1
END
--3.创建分区函数
PRINT CHAR(13)+‘--3.创建分区函数‘
DECLARE @FunValueStr NVARCHAR(MAX)=‘‘
SET @Y = YEAR(@PStartDay)
WHILE @Y <YEAR(@PEndDay)
BEGIN
SET @FunValueStr = @FunValueStr +‘‘‘‘+CAST(@Y AS NVARCHAR(4))+‘-01-01‘‘‘+‘,‘
SET @Y = @Y+1
END
SET @FunValueStr = SUBSTRING(@FunValueStr,1,len(@FunValueStr)-1)
SET @sql = ‘CREATE PARTITION FUNCTION
Fun_‘[email protected]+‘(date) AS RANGE RIGHT FOR VALUES(‘[email protected]+‘)‘
PRINT @sql + CHAR(13)
--4.创建分区方案
PRINT CHAR(13)+‘--4.创建分区方案‘
SET @Y = YEAR(@PStartDay)
SET @FGStr = ‘‘
WHILE @Y<=YEAR(@PEndDay)
BEGIN
SET @FGStr [email protected]+‘[FG_‘+CAST(@Y AS nvarchar(4))+‘],‘
SET @[email protected]+1
END
SET @FGStr=SUBSTRING(@FGStr,1,len(@FGStr)-1)
SET @sql = ‘CREATE PARTITION SCHEME Sch_‘[email protected]+‘ AS
PARTITION Fun_‘[email protected]+‘
TO(‘[email protected]+‘)‘
PRINT @sql + CHAR(13)
/***************************************************************************************
****************************************************************************************
**其他语句备案
****************************************************************************************
****************************************************************************************/
--1.为表添加分区
SET @sql=‘BEGIN TRANSACTION
CREATE CLUSTERED INDEX PIndex ON [dbo].[‘[email protected]+‘]
(
[‘[email protected]+‘]
)WITH (SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF) ON
[Sch_‘[email protected]+‘]([‘[email protected]+‘])
DROP INDEX PIndex ON [dbo].[‘[email protected]+‘]
COMMIT TRANSACTION
‘
print @sql+CHAR(13)
--新增文件组
alter database dbtest add filegroup [FG_2017]
--新增数据文件
alter database dbtest add file (name = N‘FG_2017_data‘,filename=N‘E:\Parctice\DBP\FG_2017_data.nf‘
,size=10MB,Filegrowth=100MB) to filegroup [FG_2017]
--创建分区函数
CREATE PARTITION FUNCTION Fun_DateKey(int) AS RANGE RIght FOR VALUES(20140101,20150101)
--创建分区方案
CREATE PARTITION SCHEME Sch_DateKey AS PARTITION Fun_DateKey TO ([FG_2015],[FG_2016],[FG_2017])
--修改分区函数和方案
alter partition scheme [Sch_FullDateAlternateKey] next used [FG_2017]
alter partition function [Fun_FullDateAlternateKey]() split range(‘2016-01-01‘)
--将分区的数据转移到另外一个表的分区
alter table dbtest.dbo.dimdate switch partition 2 to dbtest.dbo.dimdate_temp partition 2
--查询分区函数、分区方案、边界值的系统视图
select * from sys.partition_functions
select * from sys.partition_range_values
select * from sys.partition_schemes
以上是关于SQL Server分区脚本备份的主要内容,如果未能解决你的问题,请参考以下文章