April 18, 2005 at 4:05 am
Hi
Is it possible to delete all content in all/several tables in one databases at the same time.
I just want to empty them, not delete the tables from the server.
The reason for this is that the database is widley used and it has now become huge. I update this datbase each day, and several tabels two times a day.
I want to start with a blank database to see how much space it allocates.
Regards
DJ
April 18, 2005 at 4:31 am
Instead of deleting all/several tables, I would rather script all db objects into one file, run this under a different db name against another server and see what size I get there.
--
Frank Kalis
Microsoft SQL Server MVP
Webmaster: http://www.insidesql.org/blogs
My blog: http://www.insidesql.org/blogs/frankkalis/[/url]
April 18, 2005 at 5:42 am
Be very sure that you are in the correct database when you run this.
DECLARE @sObjectName NVARCHAR(128)
--Loop through all table objects.
SET@sObjectName = ''
WHILE@sObjectName IS NOT NULL
BEGIN
SELECT@sObjectName = MIN ( Name )
FROMSysObjects
WHEREType='U' AND NAME > @sObjectName
IF @sObjectName IS NOT NULL
BEGIN
TRUNCATE TABLE @sObjectName
PRINT '....Truncating table ' + @sObjectName
END
END
April 18, 2005 at 5:53 am
However, still you won't get reliable results on how many space your db allocates. TRUNCATE does not compact the database, so that result is skewed.
--
Frank Kalis
Microsoft SQL Server MVP
Webmaster: http://www.insidesql.org/blogs
My blog: http://www.insidesql.org/blogs/frankkalis/[/url]
April 18, 2005 at 6:57 am
Yes u can delete all the tables and empty the logs ,
But as frank said most reliable and easy way is to recreate database from the begining.
In that way you have the confidence that the database is new. don't you?
My Blog:
April 18, 2005 at 11:45 am
you may want to have a look at sp_create_removable
hth
* Noel
April 19, 2005 at 6:41 am
I think that due to foreign key constraints, a cursor is not the right solution.
i would recommend adapting the Hierarchy2 script from this site.
http://www.sqlservercentral.com/scripts/contributions/759.asp
it returns all tables in the hierachial (?sp?) order of the foreign keys, so you can then delete the data in the tables in the proper order.
Lowell
April 19, 2005 at 1:29 pm
The script Lowell referenced appears flawed. It gave me incorrect results.
This script creates the truncate/delete commands in order with respect to foreign keys as Dan requested, but I think Frank's answer (script to new DB) was the best idea.
The script only generates the commands to do the deletion. I don't like putting EXEC in these things and turning them loose without checking what they'll do.
set nocount on
declare @level tinyint
set @level = 0
create table #tables (
id int not null primary key clustered,
TableName varchar(255) not null,
Level tinyint not null)
insert into #tables (id, TableName, Level)
select id, '[' + user_name(uid) + '].[' + rtrim(name) + ']' as TableName, 0
from sysobjects where xtype = 'U' and status > 0
while @@rowcount > 0 begin
set @level = @level + 1
update rt set Level = @level
from #tables rt
inner join sysreferences fk on fk.rkeyid = rt.id
inner join #tables ft on ft.id = fk.fkeyid
where ft.Level = @level - 1
end
print 'USE ' + DB_NAME() + '
'
select 'TRUNCATE TABLE ' + TableName from #tables where level = 0
select 'DELETE ' + TableName from #tables where level > 0 order by level
drop table #tables
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply