您好,登錄后才能下訂單哦!
這篇文章主要介紹SQL Server如何進行表分區刪除,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
刪除分區又稱為合并分區,簡單地講就是將多個分區的數據進行合并。現以表Sales.SalesOrderHeader
作為示例,演示如何進行表分區刪除。
重要的事情說三遍:備份數據庫!備份數據庫!備份數據庫!
SELECT * FROM SYS.PARTITION_FUNCTIONS --分區函數 SELECT * FROM SYS.PARTITION_RANGE_VALUES --分區方案
SELECT $PARTITION.SalesOrderHeader_OrderDate(OrderDate) AS NUMBER,COUNT(1) AS COUNT FROM [Sales].[SalesOrderHeader] GROUP BY $PARTITION.SalesOrderHeader_OrderDate(OrderDate)
分區表中有數據時,是不能夠刪除分區方案和分區函數的,只能將數據先移到其它表中,再刪除。
ALTER PARTITION FUNCTION SalesOrderHeader_OrderDate() MERGE RANGE('2011-01-01 00:00:00.000') ALTER PARTITION FUNCTION SalesOrderHeader_OrderDate() MERGE RANGE('2012-01-01 00:00:00.000') ALTER PARTITION FUNCTION SalesOrderHeader_OrderDate() MERGE RANGE('2013-01-01 00:00:00.000') ALTER PARTITION FUNCTION SalesOrderHeader_OrderDate() MERGE RANGE('2014-01-01 00:00:00.000')
ALTER TABLE [Sales].[SalesOrderHeader] ADD CONSTRAINT [PK_SalesOrderHeader_SalesOrderID] PRIMARY KEY NONCLUSTERED ( [SalesOrderID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
ALTER TABLE [Sales].[SalesOrderHeader] DROP CONSTRAINT [PK_SalesOrderHeader_SalesOrderID]
CREATE TABLE [Sales].[SalesOrderHeader_Temp]( [SalesOrderID] [INT] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL, [RevisionNumber] [TINYINT] NOT NULL, [OrderDate] [DATETIME] NOT NULL, [DueDate] [DATETIME] NOT NULL, [ShipDate] [DATETIME] NULL, [Status] [TINYINT] NOT NULL, [OnlineOrderFlag] [dbo].[Flag] NOT NULL, [SalesOrderNumber] AS (ISNULL(N'SO'+CONVERT([NVARCHAR](23),[SalesOrderID]),N'*** ERROR ***')), [PurchaseOrderNumber] [dbo].[OrderNumber] NULL, [AccountNumber] [dbo].[AccountNumber] NULL, [CustomerID] [INT] NOT NULL, [SalesPersonID] [INT] NULL, [TerritoryID] [INT] NULL, [BillToAddressID] [INT] NOT NULL, [ShipToAddressID] [INT] NOT NULL, [ShipMethodID] [INT] NOT NULL, [CreditCardID] [INT] NULL, [CreditCardApprovalCode] [VARCHAR](15) NULL, [CurrencyRateID] [INT] NULL, [SubTotal] [MONEY] NOT NULL, [TaxAmt] [MONEY] NOT NULL, [Freight] [MONEY] NOT NULL, [TotalDue] AS (ISNULL(([SubTotal]+[TaxAmt])+[Freight],(0))), [Comment] [NVARCHAR](128) NULL, [rowguid] [UNIQUEIDENTIFIER] ROWGUIDCOL NOT NULL, [ModifiedDate] [DATETIME] NOT NULL )
1)對著原表Sales.SalesOrderHeader
點擊"右鍵"->"設計"。
2)點擊菜單欄"視圖"->"屬性窗口"。
3)將數據空間類型更改為"文件組",常規數據空間規范默認為"PRIMARY"。
ALTER TABLE [Sales].[SalesOrderHeader] SWITCH PARTITION 1 TO [Sales].[SalesOrderHeader_Temp] PARTITION 1
ALTER TABLE [Sales].[SalesOrderHeader_Temp] ADD CONSTRAINT [PK_SalesOrderHeader_SalesOrderID] PRIMARY KEY NONCLUSTERED ( [SalesOrderID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
DROP TABLE Sales.SalesOrderHeader
DROP PARTITION SCHEME SalesOrderHeader_OrderDate DROP PARTITION FUNCTION SalesOrderHeader_OrderDate
EXEC SP_RENAME '[Sales].[SalesOrderHeader_Temp]','SalesOrderHeader'
以上是“SQL Server如何進行表分區刪除”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。