SQL Server uses cursors to bulk empty data tables

Source: Internet
Author: User

Just spit it out, because the company is going to deploy a whole new system for new customers, but the company doesn't have an empty library, so it can only back up the databases that are running on the wire, and then empty the data.

Here's a look at what I wrote when I emptied the database. A method of bulk emptying a data table

Idea: Query out all the tables under the library filter out the tables that need to be emptied according to the table name (the system-related data table is named as regular)

Below, on the code

1  Use [DataBase]2 GO3 DECLARE @name varchar( -)4 DECLARE @count int5 Set @count=06 DECLAREContact_cursorCURSOR  for7 SelectName fromsysobjects8 whereXtype='u'  andName like 'tb_xxx_%'--filter table names based on criteria9 Ten OPENContact_cursor One  A  - FETCH NEXT  fromContact_cursor -  into @name--Table name the  -  while @ @FETCH_STATUS = 0 - BEGIN -    Print 'The table has been emptied'+@name +    Set @count=@count+1 -    exec('TRUNCATE TABLE'+@name) +    FETCH NEXT  fromContact_cursor A     into @name at END - Print 'process completed, total data table emptied'+Convert(varchar( -),@count)+'a' - CLOSEContact_cursor - deallocateContact_cursor - GO

SQL Server uses cursors to bulk empty data tables

Related Article

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.