Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

Automate Range Right Partition Management with PowerShell

By Hugh Scott,

In our environment, we have several partitioned tables that share a single partition scheme (with a range right, date partition function). Our objective is to keep X months of data in the partitioned tables (where X is determined by environment). I wanted a simple way to split off the trailing month of data from each partitioned table, merge the trailing boundary and then clean up the (now empty) file groups.

This Powershell script finds the tables associated with the Partition Scheme, creates copies of the tables, splits data from the targeted partition boundary into table copies, then (optionally) drops the stage tables, merges the partition boundary, and drops the associated file group.

This was my first realy foray into PowerShell. Some of the concepts were difficult to get my head around, but once I had the basics, it was actually pretty straightforward.

A huge chunk of this was adapted from Stuart Ozer's PartitionManager C# class library. The only reason I decided not to use that particular implementation was the requirement to install components from SQL 2008 (we are still lagging behind on SQL 2005). Also, I wanted to be able to manage multiple tables for one partition scheme without calling the script several times.

This has only been tested on SQL 2005 (not SQL 2008).

 

The Partition Function is defined as:

/****** Object: PartitionFunction [pf_FACT_DATA_DATE] Script Date: 12/17/2009 12:23:54 ******/
CREATE PARTITION FUNCTION [pf_FACT_DATA_DATE](datetime) AS RANGE RIGHT FOR
VALUES (
N'2007-04-01T00:00:00', N'2007-05-01T00:00:00', N'2007-06-01T00:00:00', N'2007-07-01T00:00:00', N'2007-08-01T00:00:00', N'2007-09-01T00:00:00', N'2007-10-01T00:00:00', N'2007-11-01T00:00:00', N'2007-12-01T00:00:00',
N'2008-01-01T00:00:00', N'2008-02-01T00:00:00', N'2008-03-01T00:00:00', N'2008-04-01T00:00:00', N'2008-05-01T00:00:00', N'2008-06-01T00:00:00', N'2008-07-01T00:00:00', N'2008-08-01T00:00:00', N'2008-09-01T00:00:00', N'2008-10-01T00:00:00', N'2008-11-01T00:00:00', N'2008-12-01T00:00:00',
N'2009-01-01T00:00:00', N'2009-02-01T00:00:00', N'2009-03-01T00:00:00', N'2009-04-01T00:00:00', N'2009-05-01T00:00:00', N'2009-06-01T00:00:00', N'2009-07-01T00:00:00', N'2009-08-01T00:00:00', N'2009-09-01T00:00:00', N'2009-10-01T00:00:00', N'2009-11-01T00:00:00', N'2009-12-01T00:00:00')

The Partition Scheme is define as:

/****** Object: PartitionScheme [ps_FACT_DATA_DATE] Script Date: 12/17/2009 12:24:53 ******/
CREATE PARTITION SCHEME [ps_FACT_DATA_DATE] AS PARTITION [pf_FACT_DATA_DATE] TO (
[FACT_EMPTY], [FACT_2007_M04], [FACT_2007_M05], [FACT_2007_M06], [FACT_2007_M07], [FACT_2007_M08], [FACT_2007_M09], [FACT_2007_M10], [FACT_2007_M11], [FACT_2007_M12],
[FACT_2008_M01], [FACT_2008_M02], [FACT_2008_M03], [FACT_2008_M04], [FACT_2008_M05], [FACT_2008_M06], [FACT_2008_M07], [FACT_2008_M08], [FACT_2008_M09], [FACT_2008_M10], [FACT_2008_M11], [FACT_2008_M12],
[FACT_2009_M01], [FACT_2009_M02], [FACT_2009_M03], [FACT_2009_M04], [FACT_2009_M05], [FACT_2009_M06], [FACT_2009_M07], [FACT_2009_M08], [FACT_2009_M09], [FACT_2009_M10], [FACT_2009_M11], [FACT_2009_M12])

Total article views: 1501 | Views in the last 30 days: 6
 
Related Articles
FORUM

Partition Scheme and Fundtion

Partition Scheme and Fundtion

FORUM

Altering Partition Scheme and Function

How to Alter Partition Scheme and Function

SCRIPT

Automated Script to Partition Tables

This script is used to print the alter statement and also partition the table with the respective pa...

FORUM

Partitioning issue

Unexpected(?) behavior after 'Alter partition scheme' statement

BLOG

Partitioning Strangeness

Ran across this in a recent class while covering partitioning. Start by creating a standard partitio...

Tags
partitioning    
powershell    
 
Contribute

Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones
Editor, SQLServerCentral.com

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones