如果由于外键约束删除
table
失败,则先删除所有约束:
--/1th Step ********** Remove foreign KEY constraints for all tables *************************/ 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 *************************/ Usedatabase name (the name of the database where you want to delete the table)GODeclare @sql varchar(8000) while(Select Count(*) fromsysobjectswhereType='U')>0beginSELECT @sql='drop table' +name fromsysobjectsWHERE(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 type= ' U ' to the where type= ' P ', drop table changed to drop Procedure.
SQL Server deletes all table scripts