我有一个分区表“TableA”。 我在列名称“RecorDate”(DATE 类型)上创建了一个分区。 有过去 6 个月的记录,我不想保存它们,我想删除它们。
是否有一种简单的方法来删除范围值或特定分区号(甚至是所有范围,例如“01-01-2014”到“01-01-2015”)。
类似于 ORACLE,TRUNCATE TABLE WITH(PARTITION(x,y to z))
。
顺便说一句 我知道 switch partition 命令,将范围移动到另一个表,然后执行命令
TRUNCATE TABLE TableNameb ;
GO
10倍
最佳答案
TRUNCATE WITH PARTITION将随 SQL Server 2016 一起提供
WITH ( PARTITIONS ( { | } [ , ...n ] ) )
Applies to: SQL Server (SQL Server 2016 Community Technology Preview 2 (CTP2) through current version). Specifies the partitions to truncate or from which all rows are removed. If the table is not partitioned, the WITH PARTITIONS argument
will generate an error. If the WITH PARTITIONS clause is not provided, the entire table will be truncated.
<partition_number_expression> can be specified in the following ways: Provide the number of a partition, for example: WITH (PARTITIONS (2)) Provide the partition numbers for several individual partitions separated by commas, for example: WITH (PARTITIONS (1, 5)) Provide both ranges and individual partitions, for example: WITH (PARTITIONS (2, 4, 6 TO 8)) <range> can be specified as partition numbers separated by the word TO, for example: WITH (PARTITIONS (6 TO 8))
示例:
TRUNCATE TABLE dbo.PartitionedTable WITH (PARTITIONS (2, 4, 6 TO 8))
2016 年之前的 SQL Server
今天您可以使用 this :
As you note, there is a way to do this today by moving the partition to a second table through ALTER TABLE SWITCH and then TRUNCATE the second table. If both tables are part of the same filegroup, this should actually be a low-risk operation as no data is moved, we just update the metadata.
关于sql - 有没有办法从分区表中删除范围值(sql server 2012),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32325594/