sql - 有没有办法从分区表中删除范围值(sql server 2012)

标签 sql sql-server tsql sql-server-2012 database-partitioning

我有一个分区表“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.

Example TABLE SWITCH

关于sql - 有没有办法从分区表中删除范围值(sql server 2012),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32325594/

相关文章:

sql-server - SQL Server 中的事务大小限制

sql-server - MS SQL 存储过程问题

sql-server - 如何使用 T-SQL 将表中的记录转换为 xml 格式?

mysql - 使用 MySQL 的数据透视表

sql - 具有间隙约束的 BigQuery 平均时间

c# - 使用 CLR 返回表

sql-server - 如何避免在 SQL 中存储像 <option/> 这样的 XML <option></option>?

sql - 为什么负整数要比正整数多?

sql - 如何将值从一个外键迁移到另一个 SQL

c# - 使用 LINQ 处理大型 SQL 查询