Emptying data for all tables in a database using SQL statements

Source: Internet
Author: User

Recently found that the database is too large, the space is not enough, so it is intended to complete the database of data cleanup, but the table is very many, one by one empty, it is really troublesome, so you want to use SQL statements to clear all the data at once. Three methods were found to clear. The database used is MS SQL SERVER.
1. Search for all table names, constructed as an SQL statement

650) this.width=650; "src=" Http://image57.360doc.com/DownloadImg/2012/12/1910/29021121_1 "align=" Top "/>Declare @trun_name varchar(8000)
650) this.width=650; "src=" Http://image57.360doc.com/DownloadImg/2012/12/1910/29021121_1 "align=" Top "/>Set @trun_name="'
650) this.width=650; "src=" Http://image57.360doc.com/DownloadImg/2012/12/1910/29021121_1 "align=" Top "/>Select @trun_name=@trun_name + 'TRUNCATE TABLE' + [name] + ' ' fromsysobjectswherextype='U' andStatus> 0
650) this.width=650; "src=" Http://image57.360doc.com/DownloadImg/2012/12/1910/29021121_1 "align=" Top "/>exec (@trun_name)

This method is suitable for tables where there are not very many cases, otherwise the number of tables is too large to exceed the length of the string and cannot be completely cleaned up.
2. Clean all tables with cursors

650) this.width=650; "src=" Http://image57.360doc.com/DownloadImg/2012/12/1910/29021121_1 "align=" Top "/>Declare @trun_name varchar( -)
650) this.width=650; "src=" Http://image57.360doc.com/DownloadImg/2012/12/1910/29021121_1 "align=" Top "/>DeclareName_cursorcursor for
650) this.width=650; "src=" Http://image57.360doc.com/DownloadImg/2012/12/1910/29021121_1 "align=" Top "/>Select 'TRUNCATE TABLE' +name fromsysobjectswherextype='U' andStatus> 0
650) this.width=650; "src=" Http://image57.360doc.com/DownloadImg/2012/12/1910/29021121_1 "align=" Top "/>OpenName_cursor
650) this.width=650; "src=" Http://image57.360doc.com/DownloadImg/2012/12/1910/29021121_1 "align=" Top "/>Fetch Next fromName_cursor into @trun_name
650) this.width=650; "src=" Http://image57.360doc.com/DownloadImg/2012/12/1910/29021121_1 "align=" Top "/> while @ @FETCH_STATUS = 0
650) this.width=650; "src=" Http://image57.360doc.com/DownloadImg/2012/12/1910/29021121_1 "align=" Top "/>begin
650) this.width=650; "src=" Http://image57.360doc.com/DownloadImg/2012/12/1910/29021121_1 "align=" Top "/>exec (@trun_name)
650) this.width=650; "src=" Http://image57.360doc.com/DownloadImg/2012/12/1910/29021121_1 "align=" Top "/>Print 'truncated table' + @trun_name
650) this.width=650; "src=" Http://image57.360doc.com/DownloadImg/2012/12/1910/29021121_1 "align=" Top "/>Fetch Next fromName_cursor into @trun_name
650) this.width=650; "src=" Http://image57.360doc.com/DownloadImg/2012/12/1910/29021121_1 "align=" Top "/>End
650) this.width=650; "src=" Http://image57.360doc.com/DownloadImg/2012/12/1910/29021121_1 "align=" Top "/>CloseName_cursor
650) this.width=650; "src=" Http://image57.360doc.com/DownloadImg/2012/12/1910/29021121_1 "align=" Top "/>deallocateName_cursor
650) this.width=650; "src=" Http://image57.360doc.com/DownloadImg/2012/12/1910/29021121_1 "align=" Top "/>

This is my own construction, can be called as a stored procedure, able to empty all the table data at once, and can also be selected to clear the table.
3. Leveraging Microsoft's undisclosed stored procedures

650) this.width=650; "src=" Http://image57.360doc.com/DownloadImg/2012/12/1910/29021121_1 "align=" Top "/> EXEC sp_msforeachtable "truncate table ?"

This method can empty all tables at once, but cannot add filters.


This article is from "Ghost" blog, please make sure to keep this source http://caizi.blog.51cto.com/5234706/1565875

Emptying data for all tables in a database using SQL statements

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.