You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[!INCLUDE [SQL Server Azure SQL Database](../../includes/applies-to-version/sql-asdb.md)]
17
-
You can change the way a table or index is partitioned in [!INCLUDE[ssnoversion](../../includes/ssnoversion-md.md)] by adding or subtracting the number of partitions specified, in increments of 1, in the partition function of the partitioned table or index by using [!INCLUDE[tsql](../../includes/tsql-md.md)]. When you add a partition, you do so by "splitting" an existing partition into two partitions and redefining the boundaries of the new partitions. When you drop a partition, you do so by "merging" the boundaries of two partitions into one. This last action repopulates one partition and leaves the other partition unassigned.
15
+
# Modify a partition function
16
+
[!INCLUDE [SQL Server Azure SQL Database Azure SQL Managed Instance](../../includes/applies-to-version/sql-asdb-asdbmi.md)]
17
+
18
+
You can change the way a table or index is partitioned in SQL Server, Azure SQL Database, and Azure SQL Managed Instance by adding or subtracting the number of partitions specified, in increments of 1, in the partition function of the partitioned table or index by using [!INCLUDE[tsql](../../includes/tsql-md.md)]. When you add a partition, you do so by "splitting" an existing partition into two partitions and redefining the boundaries of the new partitions. When you drop a partition, you do so by "merging" the boundaries of two partitions into one. This last action repopulates one partition and leaves the other partition unassigned. Review [best practices](../../t-sql/statements/alter-partition-function-transact-sql.md#best-practices) before modifying a partition function.
18
19
19
20
> [!CAUTION]
20
-
> More than one table or index can use the same partition function. When you modify a partition function, you affect all of them in a single transaction. Check the partition function's dependencies before modifying it.
21
-
22
-
**In This Topic**
23
-
24
-
-**Before you begin:**
25
-
26
-
[Limitations and Restrictions](#Restrictions)
27
-
28
-
[Security](#Security)
29
-
30
-
-**To modify a partition function, using:**
31
-
32
-
[SQL Server Management Studio](#SSMSProcedure)
33
-
34
-
[Transact-SQL](#TsqlProcedure)
35
-
36
-
## <aname="BeforeYouBegin"></a> Before You Begin
21
+
> More than one table or index can use the same partition function. When you modify a partition function, you affect all of them in a single transaction. Check the [partition function's dependencies](#query-partitioned-objects-in-a-database) before modifying it.
22
+
23
+
Table partitioning is also available in dedicated SQL pools in Azure Synapse Analytics, with some syntax differences. Learn more in [Partitioning tables in dedicated SQL pool](/azure/synapse-analytics/sql-data-warehouse/sql-data-warehouse-tables-partition).
37
24
38
-
###<aname="Restrictions"></a> Limitations and Restrictions
25
+
## <aname="Restrictions"></a> Limitations
39
26
40
27
- ALTER PARTITION FUNCTION can only be used for splitting one partition into two, or for merging two partitions into one. To change the way a table or index is partitioned (from 10 partitions to 5, for example), you can use any one of the following options:
41
28
42
-
- Create a new partitioned table with the desired partition function, and then insert the data from the old table into the new table by using either an INSERT INTO ... SELECT FROM [!INCLUDE[tsql](../../includes/tsql-md.md)] statement or the **Manage Partition Wizard** in [!INCLUDE[ssManStudioFull](../../includes/ssmanstudiofull-md.md)].
29
+
- Create a new partitioned table with the desired partition function, and then insert the data from the old table into the new table by using either an INSERT INTO ... SELECT FROM [!INCLUDE[tsql](../../includes/tsql-md.md)] statement or the **Manage Partition Wizard** in [SQL Server Management Studio (SSMS)](../../ssms/sql-server-management-studio-ssms.md).
43
30
44
-
- Create a partitioned clustered index on a heap.
31
+
- Create a partitioned [clustered index](../../t-sql/statements/create-index-transact-sql.md) on a heap.
45
32
46
33
> [!NOTE]
47
34
> Dropping a partitioned clustered index results in a partitioned heap.
48
35
49
-
- Drop and rebuild an existing partitioned index by using the [!INCLUDE[tsql](../../includes/tsql-md.md)] CREATE INDEX statement with the DROP EXISTING = ON clause.
36
+
- Drop and rebuild an existing partitioned index by using the [!INCLUDE[tsql](../../includes/tsql-md.md)][CREATE INDEX statement with the DROP EXISTING = ON](../../t-sql/statements/create-index-transact-sql.md#drop_existing---on--off-) clause.
50
37
51
38
- Perform a sequence of ALTER PARTITION FUNCTION statements.
52
39
53
-
-[!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] does not provide replication support for modifying a partition function. If you want to make changes to a partition function in the publication database, you must do this manually in the subscription database.
40
+
-The database engine does not provide replication support for modifying a partition function. If you want to make changes to a partition function in the publication database, you must do this manually in the subscription database.
54
41
55
42
- All filegroups that are affected by ALTER PARTITION FUNCTION must be online.
56
43
57
-
### <aname="Security"></a> Security
58
-
59
-
#### <aname="Permissions"></a> Permissions
44
+
## <aname="Permissions"></a> Permissions
60
45
Any one of the following permissions can be used to execute ALTER PARTITION FUNCTION:
61
46
62
47
- ALTER ANY DATASPACE permission. This permission defaults to members of the **sysadmin** fixed server role and the **db_owner** and **db_ddladmin** fixed database roles.
63
48
64
49
- CONTROL or ALTER permission on the database in which the partition function was created.
65
50
66
51
- CONTROL SERVER or ALTER ANY DATABASE permission on the server of the database in which the partition function was created.
67
-
68
-
## <aname="SSMSProcedure"></a> Using SQL Server Management Studio
69
-
**To modify a partition function:**
70
-
71
-
This specific action cannot be performed using [!INCLUDE[ssManStudioFull](../../includes/ssmanstudiofull-md.md)]. In order to modify a partition function, you must first delete the function and then create a new one with the desired properties using the Create Partition Wizard. For more information, see
72
-
73
-
#### To delete a partition function
74
-
75
-
1. Expand the database where you want to delete the partition function and then expand the **Storage** folder.
76
-
77
-
2. Expand the **Partition Functions** folder.
78
-
79
-
3. Right-click the partition function you want to delete and select **Delete**.
80
-
81
-
4. In the **Delete Object** dialog box, ensure that the correct partition function is selected, and then click **OK**.
82
52
83
-
## <aname="TsqlProcedure"></a> Using Transact-SQL
84
-
85
-
#### To split a single partition into two partitions
86
-
87
-
1. In **Object Explorer**, connect to an instance of [!INCLUDE[ssDE](../../includes/ssde-md.md)].
53
+
## Query partitioned objects in a database
54
+
55
+
The following query lists all partitioned objects in a database. This can be used to check the dependencies for a partition function before modifying it.
56
+
57
+
```sql
58
+
SELECT
59
+
PF.nameAS PartitionFunction,
60
+
ds.nameAS PartitionScheme,
61
+
OBJECT_SCHEMA_NAME(si.object_id) as SchemaName,
62
+
OBJECT_NAME(si.object_id) AS PartitionedTable,
63
+
si.nameas IndexName
64
+
FROMsys.indexesAS si
65
+
JOINsys.data_spacesAS ds
66
+
ONds.data_space_id=si.data_space_id
67
+
JOINsys.partition_schemesAS PS
68
+
ONPS.data_space_id=si.data_space_id
69
+
JOINsys.partition_functionsAS PF
70
+
ONPF.function_id=PS.function_id
71
+
WHEREds.type='PS'
72
+
AND OBJECTPROPERTYEX(si.object_id, 'BaseType') ='U'
73
+
ORDER BY PartitionFunction, PartitionScheme, SchemaName, PartitionedTable;
74
+
```
75
+
76
+
## Split a partition with Transact-SQL
88
77
89
-
2.On the Standard bar, click **New Query**.
78
+
1.In **Object Explorer**, connect to your target database.
90
79
91
-
3.Copy and paste the following example into the query window and click **Execute**.
80
+
2.On the Standard bar, select **New Query**.
92
81
93
-
```
94
-
-- Look for a previous version of the partition function "myRangePF1" and deletes it if it is found.
82
+
3. Copy and paste the following example into the query window and select **Execute**.
83
+
84
+
This example:
85
+
- Checks for a previous version of the partition function `myRangePF1` and deletes it if it is found.
86
+
- Creates a partition function called `myRangePF1` that partitions a table into four partitions.
87
+
- Splits the partition between boundary_values 100 and 1000 to create a partition between boundary_values 100 and 500 and a partition between boundary_values 500 and 1000.
88
+
89
+
```sql
95
90
IF EXISTS (SELECT*FROMsys.partition_functions
96
91
WHERE name ='myRangePF1')
97
92
DROP PARTITION FUNCTION myRangePF1;
98
-
GO
99
-
-- Create a new partition function called "myRangePF1" that partitions a table into four partitions.
93
+
GO
94
+
100
95
CREATE PARTITION FUNCTION myRangePF1 (int)
101
96
AS RANGE LEFT FOR VALUES ( 1, 100, 1000 );
102
97
GO
103
-
--Split the partition between boundary_values 100 and 1000
104
-
--to create two partitions between boundary_values 100 and 500
105
-
--and between boundary_values 500 and 1000.
98
+
106
99
ALTER PARTITION FUNCTION myRangePF1 ()
107
100
SPLIT RANGE (500);
108
101
```
109
102
110
-
#### To merge two partitions into one partition
103
+
## Merge two partitions with Transact-SQL
111
104
112
-
1. In **Object Explorer**, connect to an instance of [!INCLUDE[ssDE](../../includes/ssde-md.md)].
105
+
1. In**Object Explorer**, connect to your target database.
113
106
114
-
2. On the Standard bar, click **New Query**.
107
+
2. On the Standard bar, select**New Query**.
115
108
116
-
3. Copy and paste the following example into the query window and click **Execute**.
117
-
118
-
```
119
-
-- Look for a previous version of the partition function "myRangePF1" and deletes it if it is found.
109
+
3. Copy and paste the following example into the query window andselect**Execute**.
110
+
111
+
This example:
112
+
- Checks if a previous version of the partition function `myRangePF1` exists, and deletes it if it is found.
113
+
- Creates a partition function called `myRangePF1` with three boundary values, which will result in four partitions.
114
+
- Merges the partition between boundary_values 1and100 with the partition between boundary_values 100and1,000.
115
+
- This results in the partition function `myRangePF1`having two boundary points, 1and1,000.
116
+
117
+
```sql
120
118
IF EXISTS (SELECT * FROM sys.partition_functions
121
119
WHERE name = 'myRangePF1')
122
120
DROP PARTITION FUNCTION myRangePF1;
123
-
GO
124
-
-- Create a new partition function called "myRangePF1" that partitions a table into four partitions.
121
+
GO
122
+
125
123
CREATE PARTITION FUNCTION myRangePF1 (int)
126
124
AS RANGE LEFT FOR VALUES ( 1, 100, 1000 );
127
125
GO
128
-
--Merge the partitions between boundary_values 1 and 100
129
-
--and between boundary_values 100 and 1000 to create one partition
130
-
--between boundary_values 1 and 1000.
126
+
131
127
ALTER PARTITION FUNCTION myRangePF1 ()
132
128
MERGE RANGE (100);
133
129
```
130
+
131
+
## Delete a partition function with SSMS
132
+
133
+
1. In**Object Explorer**, connect to your target database.
134
+
135
+
1. Expand the database where you want to delete the partition function and then expand the **Storage** folder.
134
136
135
-
For more information, see [ALTER PARTITION FUNCTION (Transact-SQL)](../../t-sql/statements/alter-partition-function-transact-sql.md).
137
+
1. Expand the **Partition Functions** folder.
136
138
139
+
1. Right-click the partition function you want to deleteandselect**Delete**.
137
140
141
+
1. In the **Delete Object** dialog box, ensure that the correct partition function is selected, and then select**OK**.
142
+
143
+
## Next steps
144
+
145
+
Learn more about related concepts in the following articles:
146
+
147
+
- [Partitioned tables and indexes](partitioned-tables-and-indexes.md)
148
+
- [Create partitioned tables and indexes](create-partitioned-tables-and-indexes.md)
149
+
- [ALTER PARTITION FUNCTION (Transact-SQL)](../../t-sql/statements/alter-partition-function-transact-sql.md)
150
+
- [Partitioning tables in dedicated SQL pool](/azure/synapse-analytics/sql-data-warehouse/sql-data-warehouse-tables-partition)
0 commit comments