SQL Server Automation partition

Source: Internet
Author: User
Tags filegroup

1. New File Group
ALTER DATABASE [Test] ADD FILEGROUP FG2010 ALTER DATABASE [Test] ADD FILEGROUP FG2011 ALTER DATABASE [Test] ADD FILEGROUP Fgall

2. New Partition file

ALTER DATABASE [Test] ADD FILE(NAME='File2010', FILENAME=N'D:\Program files\data\file2010.ndf') toFILEGROUP FG2010ALTER DATABASE [Test] ADD FILE(NAME='File2011', FILENAME=N'D:\Program files\data\file2011.ndf') toFILEGROUP FG2011ALTER DATABASE [Test] ADD FILE(NAME='Fileall', FILENAME=N'D:\Program files\data\fileall.ndf') toFILEGROUP Fgall

3. Create a partition function

--This partition function creates 3 partitions (<=2010-01-01, 2010-01-01~2011-01-01,>2011-01-01)
--range left identifies the boundary value

CREATE FUNCTION ORDERPARTITIONFN (datetime)as left forVALUES('  2010-01-01','2011-01-01')

4. Create a partition scheme

CREATE PARTITION SCHEME orderpartitionscheme  as PARTITION ORDERPARTITIONFN  to (    FG2010,    FG2011,    fgall)

5. Create a partitioned table

CREATE TABLE orderrecords (    uniqueidentifier,    datetime,    int -- Orderpartitionscheme is the partition schema that you just defined, in parentheses for the specified partition column on    Orderpartitionscheme (Createtime)

6. Define automatic partition operation

Perform actions on a regular basis using SQL job

DECLARE @maxValue DATETIME,    @fileGroupName VARCHAR( $),    @fileNamePath    VARCHAR( $),    @partitionFnName VARCHAR( $),    @schemeName VARCHAR( $),    @fileName   VARCHAR( $),    @sql        NVARCHAR( +)--partition FunctionSET @partitionFnName='ORDERPARTITIONFN';--Partitioning SchemeSET @schemeName='Orderpartitionscheme';--gets the current partition function maximum boundary valueSELECT @MaxValue = Convert(datetime,MAX(value)) fromSYS. Partition_range_values PRV Left JOINSys.partition_functions Fun onprv.function_id=fun.function_idWHEREFun.name=@partitionFnName--name of partition functionSET @fileGroupName='FG'+CONVERT(varchar,( Year(@MaxValue)+1))--D:\Program files\data\file2016.ndfSET @fileNamePath='D:\Program Files\data\file'+ CONVERT(varchar,( Year(@MaxValue)+1))+'. NDF'--File2016SET @fileName=N'File'+ CONVERT(varchar,( Year(@MaxValue)+1))--Add a filegroup using the ALTER statementSET @sql='ALTER DATABASE [Test] ADD FILEGROUP'+@fileGroupNamePRINT @sqlEXEC(@sql)SET @sql='ALTER DATABASE [Test] ADD FILE (name=" "+@fileName+" ", Filename=n" "+@fileNamePath+" ") to FILEGROUP'+' '+@fileGroupNamePRINT @sqlEXEC(@sql)--Modify the partition scheme to store the next new data with a single file groupSET @sql='ALTER PARTITION SCHEME ['+@schemeName+'] NEXT used'+' '+@fileGroupNamePRINT @sql;EXEC(@sql)SET @sql='ALTER PARTITION FUNCTION'+@partitionFnName+'() SPLIT RANGE (" "+Convert(varchar,DATEADD( Year,1,@maxValue), -)+" ")';Print @sql;EXEC(@sql)

SQL Server Automation partition

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.