Link to home
Start Free TrialLog in
Avatar of dowhatyoudo22
dowhatyoudo22

asked on

How to clear/delete data from a SQL 2005 database

I need to create several new databases from the backups of existing production databases. This I have done. Now I need to clear/delete the data from these newly created databases.

How can I use Management Studio to perform this task?
Avatar of Steve Wales
Steve Wales
Flag of United States of America image

I have a database with close to 2000 tables.  Sometimes (for testing) we want to do something similar.

Because this database has no foreign key constraints (third party vendor provides it that way) I can just truncate all the tables and then reload a demo dataset we have.

To generate the script to do that, I run this:

select 'truncate table '+name+';' from systables order by name;

I send the output from that query to text instead of a grid and then copy and paste the output to a new query window and run it.

I am VERY careful that I am in the database I want to clear and not the source production database when doing this.

If you've got referential integrity constraints, you could do a similar thing to disable them first.

I'm sure that you could do something very similar a lot faster using the undocumented procedure sp_msforeachtable but I like to see what's going to happen when doing something very destructive like that - I like to see what I'm running and have zero surprises.
As you want the database without data you could use Visual studio, start a new solution based on the template for SQL Server Database Projects.

Then Import the database, which means importing all database objects. You can choose what exactly is imported, eg with Logins/Users or just tables, with or without indexes, with or without triggers, etc. In the end you have a solution with sql scripts for each database object.

You could also copy the structure via the generation of a single script, simply in the Management Studio, the context menu of databases has an item "Tasks", which has a sub menu item "Generate Scripts". From there it's a few choices about the objects you want, again.

Bye, Olaf.
Instead of deleting the data from the database, create an empty database to begin with.

Right-click on the database name -> Tasks -> Generate Scripts...
choose "Select specific database objects"
check the "Tables" box
Next
click [Advanced]
make sure "Types of data to script" is set to "Schema only"
click [OK]
Save to file

Open and run that script from a new empty database.

Insert your demo dataset.
To ensure that the database is completely in tact I would go with the first one offered and truncate the tables, this is barring that you have any Foreign Keys.
If you do have Foreign Keys or you prefer a simpler life, I would suggest you use a tool such as SQ Compare to deploy all the empty tables.  You could even save the script and run it multiple times in empty databases.
Avatar of dowhatyoudo22
dowhatyoudo22

ASKER

I should have prefaced this question with the fact that I am not a DBA and have very little experience with managing SQL databases.

Looking into this further: I have four databases. Each database only has three tables. And after running a 'select * from <table name> for each database it appears that there is only data in one of the tables and that is no more than 200 rows worth.

What is a simple way to clear the data from these tables?
ASKER CERTIFIED SOLUTION
Avatar of Steve Wales
Steve Wales
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial