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