---
title: "T-SQL - Delete&Truncate tables"
author: "xxd"
date: "2008-12-23T00:19:44Z"
tags: []
slug: "t-sql-delete-truncate-tables"
draft: false
lastmod: "2008-12-23T00:19:44Z"
id: 460608
url: "https://xxd.iteye.com/blog/460608"
---

1\. Delete all tables   
DECLARE @tablename varchar(50)  
DECLARE @truncatesql varchar(255)  
DECLARE TrCun\_Cursor CURSOR FOR  
select \[name] from sysobjects where type = 'U'  
\-- or select \[name] from sysobjects where type = 'U' and name 'table\_you\_do\_NOT\_want\_to\_delete'

OPEN TrCun\_Cursor  
FETCH TrCun\_Cursor INTO   
@tablename  
WHILE(@@fetch\_status = 0)  
BEGIN  
SET @truncatesql = 'truncate table ' + @tablename  
\--exec(@truncatesql)   
PRINT @truncatesql  
FETCH TrCun\_Cursor INTO @tablename  
END  
CLOSE TrCun\_Cursor  
DEALLOCATE TrCun\_Cursor

2\. Truncate

EXEC sp\_MSforeachtable "truncate table ?"

**3. Delete and Truncate tables started with YDS**

sp\_MSforeachtable @command1 = "delete from ?",@whereand="and name like 'YDS\_%'"  
sp\_MSforeachtable @command1 = "truncate table ?",@whereand="and name like 'YDS\_%'"

![](https://blogger.googleusercontent.com/tracker/4722768579593920709-1256262481005073435?l=xxd79.blogspot.com)