Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
Log in  ::  Register  ::  Not logged in

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

Truncate or switch range of a partitioned table

By Federico Iori,

This script requires the creation of the big view in

Works with heap, clustered index, compressed tables

It is similar functionality as

But it is a bit more powerful and flexible

1)  Create an empty DEST_TABLE identical to SOURCE_TABLE

exec dbo.sp_truncate_partitions 'COPYTABLE', 'SOURCE_TABLE' , 'DEST_TABLE', null,null

This table can be used, for example, as stage table during an ETL process.

2) Like 1 , then switch the partitions from '2013-01-01' up to '2013-02-02' to DEST_TABLE

exec dbo.sp_truncate_partitions 'SWITCH', 'DIM_CONTRACT_SIM_SERIAL' , 'tmp_DIM_CONTRACT_SIM_SERIAL_ZZZ', '2013-01-01', '2013-02-02'

3) The same as 2 , then drops DEST_TABLE

exec dbo.sp_truncate_partitions 'DELETE', 'DIM_CONTRACT_SIM_SERIAL' , 'tmp_DIM_CONTRACT_SIM_SERIAL_ZZZ', '2013-01-01', '2013-02-02'

Total article views: 1287 | Views in the last 30 days: 46
Related Articles

Truncate Table Partition command in SQL Server

Procedure to automatically truncate table partition


Dynamically Truncate a Partitioned Table

Wouldn't be great if you could truncate any partition of a partitioned table with one T-SQL statemen...


SQL Server table partition switching

If you are using partitions in SQL Server, be aware that “partition switching” is a great feature to...







duplicate table    

Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones