Avatar of introlux
introlux
Flag for United Kingdom of Great Britain and Northern Ireland asked on

MS SQL Query to Delete ALL DATA

Hi Experts,

I am looking for a simple query that will clear all the data in all of the tables within a database. I am having to do table by table which is quite annoying. As MySQL has a method in only copying the structure of an existing database, where as MS SQL this is not possible and I am having to copy the whole database and then manually remove the data which is time consuming.

Any idea on this one??

Regards,

introlux
Microsoft SQL ServerMicrosoft SQL Server 2005SQL

Avatar of undefined
Last Comment
introlux

8/22/2022 - Mon
James Murrell

look up exec msforeachtable
Pratima

Pratima

Your help has saved me hundreds of hours of internet surfing.
fblack61
Dimitris

here u r
/*Disable Constraints & Triggers*/
exec sp_MSforeachtable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'
exec sp_MSforeachtable 'ALTER TABLE ? DISABLE TRIGGER ALL'
 
/*Perform delete operation on all table for cleanup*/
exec sp_MSforeachtable 'DELETE ?'
 
/*Enable Constraints & Triggers again*/
exec sp_MSforeachtable 'ALTER TABLE ? CHECK CONSTRAINT ALL'
exec sp_MSforeachtable 'ALTER TABLE ? ENABLE TRIGGER ALL'
 
/*Reset Identity on tables with identity column*/
exec sp_MSforeachtable 'IF OBJECTPROPERTY(OBJECT_ID(''?''), ''TableHasIdentity'') = 1 BEGIN DBCC CHECKIDENT (''?'',RESEED,0) END'

Open in new window

mindworks

Try this Command on query Analyzer

EXEC sp_MSforeachtable @command1 = "DELETE FROM ?"

Remember this cannot be roll back

introlux

ASKER
I assume i would use the DBNAME instead of the ?

Is that correct?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Dimitris

no,
you run the script as it is
you just run it in your DB from the query analyzer
Raja Jegan R

dankangr,
     Your comment no 23792896 is the same as I posted in this question.
https://www.experts-exchange.com/questions/24175376/how-to-clear-all-records-in-db.html

introlux,
   << I assume i would use the DBNAME instead of the ? >>

No Need to use the DBName just

USE DBname in front of that script will suffice.
introlux

ASKER
I am confused now. My DBName is called 'DBallstars'

WIll i just open new query and paste the following code:

EXEC sp_MSforeachtable @command1 = "DELETE FROM ?"

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
James Murrell

EXEC sp_MSforeachtable @command1 = "DELETE FROM DBallstars
Dimitris

rrjegan17
    What if the comment is the same? It was a useful comment and I used it, where is the problem?? You have share your comment on that question so i don't see the reason of not send it to others also. You should put a trade mark and all rights reserved if you don't want to use it. Or I should right comment posted by rrjegan17?
Really I can't see the problem here...
introlux

ASKER
I get the following error:

Msg 208, Level 16, State 1, Line 1
Invalid object name 'DBallstars'.

I get more similar errors when executing
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
James Murrell

in QA

EXEC sp_MSforeachtable @command1 = 'DELETE FROM DBallstars'


use DBallstars

Open in new window

introlux

ASKER
done the above and does not work

I get errors???????

Msg 208, Level 16, State 1, Line 1
Invalid object name 'DBallstars'.
Raja Jegan R

dankangr,
   I didn't meant that.
Instead if you have provided that Question itself, he could have gained more information right. That's my point and hence provided the link to him. Yes our codes are shared and if the same code would help others, then you can share the complete link so that the askers can refer more and gain more knowledge instead of us feeding them with the answers. Of course we can help but we need to help them more on the topic instead of just the answer alone.

Revert if you disagree with this.

introlux,
     I believe you are using SQL Server 2000 and installing SP 4 for SQL Server 2000 will fix this issue.

http://support.microsoft.com/kb/837970
http://sqlserver2000.databases.aspfaq.com/why-do-i-get-errors-about-master-spt-values.html
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Raja Jegan R

introlux

ASKER
I am using SQL Server 2005 btw

Regards,

introlux
Raja Jegan R

Then try the one below:

Use DBName

DELETE FROM schemaname.tablename

Or try whether you are able to select your table by doing

Use DBName

SELECT * FROM schemaname.tablename
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Dimitris

rrjegan17
   I didn't search for the link, I had coped the code i was interested in and I had keep it in a file on my PC.
So i think that we just have a big misunderstanding here. Never mind. We r here to help :)

Cheers ;-)

Raja Jegan R

Ok.. No probs..
As we are here to help others, we can help them at an optimal level instead of mere answer. Cheers :-)
ASKER CERTIFIED SOLUTION
introlux

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.