
--**************************************************************************************
-- Date: 07/19/2005
--
-- File: sliding.sql for Sliding Window Example
--
-- Summary: Managing a Range Partitioned Table
-- Archive transaction data for September 2003.
--
--**************************************************************************************
--
-- This file is part of the Microsoft SQL Server Code Samples.
-- Copyright (C) Microsoft Corporation. All rights reserved.
--
-- This source code is intended only as a supplement to Microsoft
-- Development Tools and/or on-line documentation. See these other
-- materials for detailed information regarding Microsoft code samples.
--
-- THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
-- KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
-- IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
-- PARTICULAR PURPOSE.
--
--**************************************************************************************
SET DATEFORMAT mdy;
GO
USE [AdventureWorks];
GO
ALTER PARTITION SCHEME TransactionsPS1
NEXT USED [PRIMARY];
GO
-- Add a new partition on the end of table TransactionHistory for August 2004.
ALTER PARTITION FUNCTION TransactionRangePF1()
SPLIT RANGE ('9/01/2004');
GO
ALTER PARTITION SCHEME TransactionArchivePS2
NEXT USED [PRIMARY];
GO
-- Add a new partition to table TransactionHistoryArchive to hold the
-- September 2003 data.
ALTER PARTITION FUNCTION TransactionArchivePF2()
SPLIT RANGE ('10/01/2003');
GO
-- You must add a check constraint to table TransactionHistory to define
-- the boundary for the first partition before switching it out.
ALTER TABLE [Production].[TransactionHistory]
ADD CONSTRAINT [CK_TransactionHistory_DateRange]
CHECK ([TransactionDate] >= '9/01/2003');
GO
-- Move the data for September 2003 from table TransactionHistory to
-- table TransactionHistoryArchive.
ALTER TABLE [Production].[TransactionHistory]
SWITCH PARTITION 1
TO [Production].[TransactionHistoryArchive] PARTITION 2;
GO
-- Merge the first two partitions of table TransactionHistory.
ALTER PARTITION FUNCTION TransactionRangePF1()
MERGE RANGE ('10/01/2003');
GO
-- Merge the September 2003 partition of table TransactionHistoryArchive
-- with the first partition.
ALTER PARTITION FUNCTION TransactionArchivePF2()
MERGE RANGE ('9/01/2003');
GO
-- Remove the date constraint on table TransactionHistory.
ALTER TABLE [Production].[TransactionHistory]
DROP CONSTRAINT [CK_TransactionHistory_DateRange];
GO
SELECT OBJECT_NAME([object_id]), * FROM [sys].[partitions]
WHERE [object_id] = OBJECT_ID('[Production].[TransactionHistory]')
ORDER BY [partition_number], [index_id];
GO
SELECT OBJECT_NAME([object_id]), * FROM [sys].[partitions]
WHERE [object_id] = OBJECT_ID('[Production].[TransactionHistoryArchive]')
ORDER BY [partition_number], [index_id];
GO
USE [master];
GO

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现