Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Administration
 Empty SQL Server 2005 database

Author  Topic 

laddu
Constraint Violating Yak Guru

332 Posts

Posted - 2011-12-15 : 10:56:50

Hi,

I want to empty the database (only data). Is there any script/SP available to that.

SQL Server 2005 version

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2011-12-15 : 11:04:21
Or you could:

Make a new db

And script out all the objects from the old one

there is a wizard for that.

Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2011-12-15 : 11:06:56
well they probably want to keep the code tables

Is there RI on the database?

How many tables?



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

laddu
Constraint Violating Yak Guru

332 Posts

Posted - 2011-12-15 : 11:27:37
106 tables. we do have RI in database.

I want to keep Parent and Child relationships, I want to delete only data
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-12-15 : 11:37:54
you've generate one by iterating through table list (use INFORMATION_SCHEMA.TABLES view for that) and build TRUNCATE TABLE query based on it

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2011-12-15 : 11:54:30
can't do TRUNCATE with tables that have RI..you could drop all of the RI and put it back after doing a TRUNCATE

Are there any tables you want to KEEP the data in?




Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

laddu
Constraint Violating Yak Guru

332 Posts

Posted - 2011-12-15 : 11:54:40
Task completed.

I used below script

-- disable all constraints
EXEC sp_msforeachtable "ALTER TABLE ? NOCHECK CONSTRAINT all"

-- delete data in all tables
EXEC sp_MSForEachTable "DELETE FROM ?"

-- enable all constraints
exec sp_msforeachtable "ALTER TABLE ? WITH CHECK CHECK CONSTRAINT all"
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2011-12-15 : 11:57:34
SELECT 'DELETE FROM ['+TABLE_SCHEMA+'].['+TABLE_NAME+']'+CHAR(13)+CHAR(10)+'GO'
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE'

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page
   

- Advertisement -