Solved

How to clear/delete data from a SQL 2005 database

Posted on 2014-02-25
8
414 Views
Last Modified: 2014-02-27
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?
0
Comment
Question by:dowhatyoudo22
8 Comments
 
LVL 22

Expert Comment

by:Steve Wales
ID: 39887231
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.
0
 
LVL 29

Expert Comment

by:Olaf Doschke
ID: 39887337
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.
0
 
LVL 34

Expert Comment

by:Brian Crowe
ID: 39887341
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.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 24

Expert Comment

by:DBAduck - Ben Miller
ID: 39887436
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.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 39887720
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.
0
 

Author Comment

by:dowhatyoudo22
ID: 39889398
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?
0
 
LVL 22

Accepted Solution

by:
Steve Wales earned 250 total points
ID: 39889406
If you're talking that level of volume, you're probably best just to delete the data from teh 3 or 4 tables you're talking about.

delete tablename;

or

truncate table tablename;

will do the trick.
0
 
LVL 29

Assisted Solution

by:Olaf Doschke
Olaf Doschke earned 250 total points
ID: 39889862
It's still very simple to generate a database script and run it.

First of all I assume you have SQL Server Management Studio and did restore the database backups with it, then you're already familiar with it a little.

You can use the wizard to generate a database script and specify the result script should be put into a new query window. Then you simply run it, and it's done. You may only change one of the first lines with USE 'databasename' to use a different database name. The script is intended to be used on a new, empty database.

So what you need to know additionally is how to create a new, empty database, that's simply CREATE DATABASE mynewdatabase and would be ok with any small database you will start with.

Be aware there are tutorials and demo databases available, like AdventureWorks. Google it.

Bye, Olaf.
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
migration MS SQL database to Oracle 30 61
hyperlink data type in SQL 3 27
transaction in asp.net, sql server 6 33
How to debug a store procedure in MS SQL 2008? 3 10
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

770 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