views:

1110

answers:

2

I'm looking into horizontal partitioning for a table that has time-series data in it. I've discovered that partitioning is much easier in 2005 than it was in 2000 but I can't seem to find this answer:

Can I add/drop columns of a partitioned table?

Are special steps required because it's partitioned?

+2  A: 

In addition to performing SWITCH operations that involve partitioned tables, ALTER TABLE can be used to change the state of the columns, constraints, and triggers of a partitioned table just like it is used for nonpartitioned tables. However, this statement cannot be used to change the way the table itself is partitioned. To repartition a partitioned table, use ALTER PARTITION SCHEME and ALTER PARTITION FUNCTION. Additionally, you cannot change the data type of a column of a partitioned table.

Read more here: http://technet.microsoft.com/en-us/library/ms190273.aspx

EJB
The 2005 version of that doc says the same thing (http://technet.microsoft.com/en-us/library/ms190273(SQL.90).aspx). I don't mean to be pedantic, but does that mean I can add/drop columns?
Michael Haren
A: 

I couldn't find a definitive answer (I found the doc @E.J. Brennan referenced to be a little dense and unclear). So I added to this example and have tested that, yes, you can add/drop columns:

USE adventureworks
go

create partition function YearPF(datetime) as range right for values ('20050101');

-- Now we need to add filegroups that will contains partitioned values
alter database adventureworks add filegroup YearFG1;
alter database adventureworks add filegroup YearFG2;

-- Now we need to add file to filegroups
alter database adventureworks add file (name = 'YearF1', filename = 'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\AdvWorksF1.ndf') to filegroup YearFG1;
alter database adventureworks add file (name = 'YearF2', filename = 'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\AdvWorksF2.ndf') to filegroup YearFG2;

-- Here we associate the partition function to 
-- the created filegroup via a Partitioning Scheme
create partition scheme YearPS as partition YearPF to (YearFG1, YearFG2)

-- Now just create a table that uses the particion scheme
create table PartitionedOrders
(
  Id int not null identity(1,1),
  DueDate DateTime not null,
) on YearPS(DueDate)

-- And now we just have to use the table!
insert into PartitionedOrders values('20020101')
insert into PartitionedOrders values('20030101')
insert into PartitionedOrders values('20040101')
insert into PartitionedOrders values('20050101')
insert into PartitionedOrders values('20060101')

-- Now we want to see where our values has falled
select *, $partition.YearPF(DueDate) from PartitionedOrders

-- see if we can add a column
ALTER TABLE PartitionedOrders ADD NewColumn INT NULL

-- add some more records, populating the new column
insert into PartitionedOrders values('20010101', 1)
insert into PartitionedOrders values('20070101', 2)

-- see that they were inserted properly
select *, $partition.YearPF(DueDate) from PartitionedOrders

ALTER TABLE PartitionedOrders DROP COLUMN NewColumn

-- see that the column dropped
select *, $partition.YearPF(DueDate) from PartitionedOrders

/* clean up
drop table PartitionedOrders
drop partition scheme YearPS;
drop partition function YearPF;
alter database adventureworks remove file YearF1;
alter database adventureworks remove file YearF2;
alter database adventureworks remove  filegroup YearFG1;
alter database adventureworks remove filegroup YearFG2;
*/
Michael Haren