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

http://www.sqlservercentral.com/scripts/Partitioning/73324/

Works with heap, clustered index, compressed tables

It is similar functionality as

http://www.sqlservercentral.com/scripts/Truncate+Table/69506/

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: 1124 | Views in the last 30 days: 6
 
Related Articles
SCRIPT

Truncate Table Partition command in SQL Server

Procedure to automatically truncate table partition

ARTICLE

Dynamically Truncate a Partitioned Table

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

BLOG

SQL Server table partition switching

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

FORUM

Partition

Partition

FORUM

Partition

Partition

Tags
duplicate table    
partitions    
script    
switch    
truncate    
 
Contribute

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
Editor, SQLServerCentral.com

Already a member? Jump in:

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