SQL Server Partition Scheme automatic maintenance script

Source: Internet
Author: User

CREATE PROC Proc_partition_deal
As
DECLARE @LatestValue DATETIME,
@LastValue DATETIME,
@EarlValue DATETIME,
@differ INT,
@New_Range_Value DATETIME,
@fileGroupName VARCHAR (200),
@sql VARCHAR (1000),
@P_Sch_Name VARCHAR (100),
@P_Fun_Name VARCHAR (100),
@P_Fun_Id INT

SET @fileGroupName = ' [PRIMARY] '
SET @P_Sch_Name = ' sche_part_date '
SET @P_Fun_Name = ' func_part_date '
SET @sql = ' '

IF not EXISTS (SELECT 1
From
Sys.partition_functions
WHERE name = @P_Fun_Name)
RETURN;

IF not EXISTS (SELECT 1
From
Sys.partition_schemes
WHERE name = @P_Sch_Name)
RETURN;

SELECT @P_Fun_Id = function_id
From
Sys.partition_functions
WHERE name = @P_Fun_Name

--Merge partition functions
SELECT @EarlValue = CONVERT (VARCHAR (), Min (value), 120)
From
Sys.partition_range_values
WHERE function_id = @P_Fun_Id

SET @sql = ' ALTER Partition FUNCTION ' + @P_Fun_Name
+ ' () MERGE Range (' '
+ CONVERT (VARCHAR), @EarlValue, 120)
+ '. 000 ' + ') '

EXEC (@sql)

--print @sql
--Increase the partitioning scheme
SET @sql = ' '
SET @sql = ' ALTER PARTITION SCHEME ' + @P_Sch_Name
+ ' NEXT used ' + @fileGroupName

EXEC (@sql)

--print @sql
--Add partition function
SELECT @LastValue = CONVERT (VARCHAR (), Min (value), 120),
@LatestValue = CONVERT (VARCHAR (), Max (value), 120)
From
(SELECT TOP 2 value
From
Sys.partition_range_values
WHERE function_id = @P_Fun_Id
ORDER by VALUE DESC) PRV

SET @differ =datediff (Dd, @LastValue, @LatestValue)
SET @New_Range_Value =dateadd (Dd, @differ, CONVERT (DATE, @LatestValue))
SET @sql = ' '
SET @sql = ' ALTER Partition FUNCTION ' + @P_Fun_Name
+ ' () Split Range (' '
+ CONVERT (VARCHAR), @New_Range_Value, 120)
+ '. 000 ' + ') '

EXEC (@sql)
--print @sql

SQL Server Partition Scheme automatic maintenance script

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.