To delete a database, or to delete all tables in the database (delete table structure and data), you need to delete the foreign key constraint between the tables before you can delete the table. such as deleting all tables in the database Db_wy:
--/1th Step ********** Delete all table foreign KEY constraints *************************/declare C1 cursor forselect ' ALTER TABLE [' + object_name (parent_ obj) + '] drop constraint [' +name+ ']; ' From sysobjectswhere xtype = ' F ' open c1declare @c1 varchar (8000) fetch next from C1 into @c1while (@ @fetch_status =0) Beginex EC (@c1) fetch next from C1 to @c1endclose c1deallocate c1--/2nd step ********** Delete all tables *************************/use db_ Wygodeclare @sql varchar (8000) while (select COUNT (*) from sysobjects where type= ' U ') >0beginselect @sql = ' drop table ' + Namefrom sysobjectswhere (type = ' U ') ORDER by ' drop table ' + nameexec (@sql) End
SQL Server deletes all tables (table structure and data)