How to clear all data from SQL Database?

How to clear all data from SQL Database?

sql

Hello people, How are you?
Today I was working on something and then came to realize that I had to clear the sql database. For a moment, I was just going on delete the entire sql database and create a new one with the table. But hey, did you know that we can clear the entire contents of the table? Suddenly, I thought to google about it, If I didn’t find the solution, I had the option to clear the DB. So I searched google “clear db sql mssql” and luckily I found the link of stackoverflow and that saved my day for not creating those tables again.

Here you go with the Sql Query to clear data in SQL Database

-- replace the <database> with your database, it chooses the the database
USE <database>
GO

-- disable referential integrity
EXEC sp_MSForEachTable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL' 
GO 

EXEC sp_MSForEachTable 'DELETE FROM ?' 
GO 

-- enable referential integrity again 
EXEC sp_MSForEachTable 'ALTER TABLE ? WITH CHECK CHECK CONSTRAINT ALL' 
GO

I hope this helps you out and save your time. Try it, I believe you will love to use those. It might be a life saver someday. Trust me, It saved mine today.

Good day and Stay Safe!

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: