Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

drop all the tables in a database

Posted on 2016-07-15
5
Medium Priority
?
72 Views
Last Modified: 2016-07-18
I need a script to drop all the tables in a database. What would be the easiest way to do that?
0
Comment
Question by:vbnetcoder
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 41713142
wouldn't it be easier to drop the database and recreate it?
1
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 41713146
or you can try:
EXEC sp_MSforeachtable @command1 = "DROP TABLE ?"

Open in new window

0
 

Assisted Solution

by:vbnetcoder
vbnetcoder earned 0 total points
ID: 41713148
Eric you might be right on dropping the database
0
 
LVL 10

Accepted Solution

by:
Jason clark earned 2000 total points
ID: 41713829
Try below script to drop all tables:

SELECT ' Drop table ' + s.NAME + '.' + t.NAME
FROM   sys.tables t
       JOIN sys.schemas s
         ON t.[schema_id] = s.[schema_id]
WHERE  t.type = 'U' 

Exec sp_executesql @sql

Open in new window


But remember If you have any foreign Keys defined between tables then first run the below query to disable all foreign keys present in your DB.

EXEC sp_msforeachtable "ALTER TABLE ? NOCHECK CONSTRAINT all"

Open in new window

0
 
LVL 8

Expert Comment

by:Jose Torres
ID: 41717390
Remember by dropping the database you loose all objects (views, stored procedures.....)
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question