"SQL Server" empty table and delete table and create table

Source: Internet
Author: User

1. Build a table

1 IF object_id (n ' table name ', n ' U ') is NULL CREATE table table name (2     ID INT IDENTITY (1, 1) PRIMARY KEY ,......);

2. Query all tables that meet the criteria

1 SELECT 2     NAME3from4    sys.objects5WHERE  6     type = ' u '7 and NAME like ' test_% ';

3. Bulk delete a table that meets the criteria

1 DECLARE2@NAME VARCHAR (50)3 While (4 EXISTS (5 SELECT6*7  from8 sysobjects9 WHERETenNAME like ' test_% ' One     ) A ) - BEGIN - SELECT the@NAME =NAME -  from - sysobjects - WHERE +NAME like ' test_% ' EXEC (' drop table ' +@NAME) -END

4. Bulk cursors empty tables that meet the criteria

1 DECLARE2@trun_name VARCHAR (50) DECLARE3 name_cursor cursor for SELECT4' TRUNCATE TABLE ' +NAME5  from6 sysobjects7 WHERE8xtype = ' U '9and NAME like ' test_% 'OPEN name_cursor FETCH NextTen  from One name_cursor into @trun_name AWhile @ @FETCH_STATUS = 0 - BEGIN -EXEC (@trun_name) print ' truncated table ' +@trun_name FETCH Next the  from - name_cursor into @trun_name -END CLOSE name_cursor deallocate name_cursor

"SQL Server" empty table and delete table and create table

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.