Link to home
Start Free TrialLog in
Avatar of venk_r
venk_rFlag for United States of America

asked on

HOW TO ALTER PARTITION FUNCTION IN SQL SERVER 2005?

I need to alter the data partition function which has the date range of one month each(3 partitions). I need to change them to contain 2 months of data instead  on each partition. What is the best approach on doing this?
Avatar of Raja Jegan R
Raja Jegan R
Flag of India image

Below query will give you the Merge statements that needs to be executed.
Hope this helps
select 'alter partition function ur_partition_fn_name () merge range (' + t1.value + ')'
from (
select t1.value, row_number() over ( order by t1.value) rnum
from sys.partition_range_values t1, sys.partition_functions t2
where t1.function_id = t2.function_id
and t2.name = 'ur_partition_fn_name' ) temp
where rnum = 1

Open in new window

Avatar of venk_r

ASKER

Iam getting the below error when I execute this
The multi-part identifier "t1.value" could not be bound.

Oops.. Small syntax mistake.
Kindly try this one out
select 'alter partition function ur_partition_fn_name () merge range (' + value + ')'
from (
select t1.value, row_number() over ( order by t1.value) rnum
from sys.partition_range_values t1, sys.partition_functions t2
where t1.function_id = t2.function_id
and t2.name = 'ur_partition_fn_name' ) temp
where rnum = 1

Open in new window

Avatar of venk_r

ASKER

This time I get one more exception
Msg 402, Level 16, State 1, Line 1
The data types varchar and sql_variant are incompatible in the add operator.
Currently you have only 3 partitions and you can manually merge the second partition.

alter partition function ur_partition_fn_name () merge range ( ur_second_partition_val)

Kindly replace ur_partition_fn_name and ur_second_partition_val to get this work out now.

Anyhow this script will be helpful to you when you have several partitions with you.
Try this one out..

Dont have access to Server now and hence haven't checked out for syntax.
Kindly revert if any error comes again.
select 'alter partition function ur_partition_fn_name () merge range (''' + convert(varchar(10), value, 101) + ''')'
from (
select t1.value, row_number() over ( order by t1.value) rnum
from sys.partition_range_values t1, sys.partition_functions t2
where t1.function_id = t2.function_id
and t2.name = 'ur_partition_fn_name' ) temp
where rnum = 2

Open in new window

Avatar of venk_r

ASKER

Actually I have 4 left date range partition
CREATE PARTITION FUNCTION Trackpartitionfn(datetime)
AS RANGE LEFT FOR VALUES

('20090108 23:59:59.997',
'20090208 23:59:59.997',
'20090308 23:59:59.997',
'20090408 23:59:59.997')

And I want to create 2 months per partition now which would like
CREATE PARTITION FUNCTION Trackpartitionfn(datetime)
AS RANGE LEFT FOR VALUES
(
'20090308 23:59:59.997',
'20090508 23:59:59.997',
'20090708 23:59:59.997',
'20090708 23:59:59.997')

How would I use merge to achieve this.?
Avatar of venk_r

ASKER

Do I need to destroy the whole partition and create it from scratch with new boundaries?
The script which I provided earlier would help you to achieve something like this

CREATE PARTITION FUNCTION Trackpartitionfn(datetime)
AS RANGE LEFT FOR VALUES

('20090108 23:59:59.997',
'20090308 23:59:59.997')

To add new more partitions you have to use SPLIT function as given below:

alter partition function Trackpartitionfn () split range ( '20090508 23:59:59.997');
alter partition scheme ur_partition_scheme NEXT USED [primary];
alter partition function Trackpartitionfn () split range ( '20090708 23:59:59.997');
alter partition scheme ur_partition_scheme NEXT USED [primary];
alter partition function Trackpartitionfn () split range ( '20090908 23:59:59.997');
alter partition scheme ur_partition_scheme NEXT USED [primary];

you have to replace ur_partition_scheme with your partition scheme name and primary with filegroup name if you have used other than primary.

Hope this helps
ASKER CERTIFIED SOLUTION
Avatar of Raja Jegan R
Raja Jegan R
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial