How to Use SQLserver to delete all tables in a database

Source: Internet
Author: User

It is convenient to delete all data tables in the database and clear the database. There are some constraints that cannot be deleted directly. You need to delete the constraints in the database first. The Code is as follows:
Copy codeThe Code is as follows:
-- Delete all constraints
DECLARE c1 cursor
Select 'alter table ['+ object_name (parent_obj) +'] drop constraint ['+ name +'];'
From sysobjects
Where xtype = 'F'
Open c1
Declare @ c1 varchar (8000)
Fetch nextfrom c1 into @ c1
While (@ fetch_status = 0)
Begin
Exec (@ c1)
Fetch nextfrom c1 into @ c1
End
Close c1
Deallocate c1
-- Delete all tables in the database
Declare @ tname varchar (8000)
Set @ tname =''
Select @ tname = @ tname + Name + ', 'From sysobjects where xtype = 'U'
Select @ tname = 'drop table' + left (@ tname, len (@ tname)-1)
Exec (@ tname)

Then clear all tables in the database.:
To delete a stored Procedure, change where xtype = 'U' to where xtype = 'p' and drop table to drop Procedure.

The xtype of sysobjects indicates the meaning.:

Each object created in the database (constraints, default values, logs, rules, stored procedures, etc.) occupies one row in the table. Each temporary object occupies one row in the table only in tempdb.

Column name data type description
Name sysname object name.
Id int Id of the object.
Xtype char (2) object type. It can be one of the following object types:
C = CHECK Constraints
D = DEFAULT value or DEFAULT Constraint
F = foreign key constraint
L = Log
FN = scalar function
IF = embedded table functions
P = Stored Procedure
PK = primary key constraint (type: K)
RF = copy and filter the Stored Procedure
S = system table
TF = table functions
TR = trigger
U = User table
UQ = UNIQUE constraint (type is K)
V = View
X = Extended Stored Procedure

User ID of the uid smallint owner object.
Info smallint is retained. For internal use only.
Status int is retained. For internal use only.
Base_schema _
Ver int is retained. For internal use only.
Replinfo int is retained. For replication.
The object ID of the parent object of parent_obj int (for example, this ID is the table ID for triggers or constraints ).
The creation date of the crdate datetime object.
Ftcatid smallint is the full-text directory identifier of all user tables registered with the full-text index. It is 0 for all user tables not registered.
Schema_ver int version number, which increases each time the schema of the table is changed.
Stats_schema _
Ver int is retained. For internal use only.
Type char (2) object type. It can be one of the following values:
C = CHECK Constraints
D = DEFAULT value or DEFAULT Constraint
F = foreign key constraint
FN = scalar function
IF = embedded table functions
K = primary key or UNIQUE constraint
L = Log
P = Stored Procedure
R = rule
RF = copy and filter the Stored Procedure
S = system table
TF = table functions
TR = trigger
U = User table
V = View
X = Extended Stored Procedure
Userstat smallint is retained.
Sysstat smallint internal status information.
Indexdel smallint is retained.
Refdate datetime is reserved for future use.
Version int is reserved for later use.
Deltrig int is retained.
Instrig int is retained.
Updtrig int is retained.
Seltrig int is retained.
Category int is used for publishing, constraints, and identifiers.
Cache smallint is retained.

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.