SQL Server deletes all tables and all stored procedures for the database

Source: Internet
Author: User
Tags scalar

Scene:In SQL Server, when you need to delete all the tables or all stored procedures, the manual method can only be deleted individually, consuming personal time, so you want to get a statement to achieve such a requirement.

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

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

1 DECLAREC1cursor  for 2     Select 'ALTER TABLE ['+ object_name(parent_obj)+ '] Drop constraint ['+Name+']; '3      fromsysobjects4     whereXtype= 'F'5 OpenC16 Declare @c1 varchar(8000)7 Fetch Next  fromC1 into @c18  while(@ @fetch_status=0)9     begin Ten         exec(@c1) One         Fetch Next  fromC1 into @c1 A     End - CloseC1 - deallocateC1 the Go

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

1  UseDatabase2 Declare @tname varchar(8000)3 Set @tname="'4 Select @tname=@tname +Name+ ','  fromsysobjectswhereXtype='U'5 Select @tname='drop table' +  Left(@tname,Len(@tname)-1)6 exec(@tname)7 Go

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 and all stored procedures for 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.