SQL Server deletes all tables in the database

Source: Internet
Author: User
Tags scalar management studio sql server management sql server management studio

SQL Server deletes all tables in the database

-------------------------------------------------------------------------------------------

Open SQL Server Management Studio and create a new query in the Open XXX. In the SQL file, enter:

EXEC@command1='Delete from? ' exec  @command1= "TRUNCATETABLE ?"

Then execute the statement.

------------------------------------------------------------------------------------------

If table failure is deleted because of a foreign KEY constraint, all constraints are removed first:

--/1th Step ********** Delete all table foreign KEY constraints *************************/

DECLAREC1cursor  forSelect 'ALTER TABLE ['+ object_name(parent_obj)+ '] Drop constraint ['+Name+']; ' fromsysobjectswhereXtype= 'F'OpenC1Declare @c1 varchar(8000)Fetch Next  fromC1 into @c1 while(@ @fetch_status=0)beginexec(@c1)Fetch Next  fromC1 into @c1EndCloseC1deallocateC1

--/2nd Step ********** Delete all tables *************************/

Use database name (the name of the database where you want to delete the table)

godeclare @sql varchar (8000) while(SelectCOUNT (*) fromsysobjectswhereType='U') >0Beginselect @sql='drop table'+namefrom sysobjectswhere (Type='U') ORDER by'drop table'+nameexec (@sql) End

Delete all stored procedures Similarly, but do not need to take the first step, just the 2nd step of the code where xtype= ' U ' change to where xtype= ' P ', drop table to drop Procedure

Sysobjects's xtype means:

Each object created within the database (constraints, default values, logs, rules, stored procedures, and so on) occupies a single row in the table. Only within tempdb, each temporary object occupies one row in the table.

Column name Data type description
Name sysname object name.
ID int object identification number.
Xtype char (2) object type. Can be one of the following object types:
C = CHECK Constraint
D = defaults or DEFAULT constraints
F = FOREIGN KEY constraint
L = Log
FN = Scalar function
IF = Inline Table function
P = Stored Procedure
PK = PRIMARY KEY constraint (type is K)
RF = copy Filter stored procedure
S = System table
TF = Table function
TR = Trigger
U = User Table
UQ = UNIQUE constraint (type is K)
V = view
X = Extended Stored Procedure

UID smallint the user ID of the owner object.
Info smallint reserved. Internal use only.
status int reserved. Internal use only.
Base_schema_
ver int reserved. Internal use only.
replinfo int reserved. For replication use.
The object identification number of the Parent_obj int parent object (for example, for a trigger or constraint, the identification number is a table ID).
Crdate the date the DateTime object was created.
Ftcatid smallint Full-text catalog identifier for all user tables registered for full-text indexing, or 0 for all user tables that are not registered.
The Schema_ver int version number, which is incremented each time the schema of the table changes.
Stats_schema_
ver int reserved. Internal use only.
Type char (2) object types. Can be one of the following values:
C = CHECK Constraint
D = defaults or DEFAULT constraints
F = FOREIGN KEY constraint
FN = Scalar function
IF = Inline Table function
K = PRIMARY KEY or UNIQUE constraint
L = Log
P = Stored Procedure
R = Rule
RF = copy Filter stored procedure
S = System table
TF = Table function
TR = Trigger
U = User Table
V = view
X = Extended Stored Procedure

Userstat smallint reserved.
Sysstat smallint internal state information.
Indexdel smallint reserved.
Refdate datetime is reserved for later use.
version int is reserved for later use.
Deltrig int reserved.
Instrig int reserved.
Updtrig int reserved.
Seltrig int reserved.
Category int is used for publishing, constraining, and identifying.
Cache smallint reserved.

SQL Server deletes all tables in the database

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.