Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Easiest Clear All

Status
Not open for further replies.

bont

Programmer
Sep 7, 2000
200
US
I am looking for the easiest way to clear all of my tables in my SQL database. Is there some kind of internal funtion that may suite me, or should I create a function or procedure. I am a newbe so if you have any examples of how I can cycle through all of the table in the current DB and then clear them, I would appreciate it.

thanx
 
create table #myTables (
TableID int primary key identity (1,1)
,TableName varchar(200)
)
insert into #myTables ( TableName )
select Name from sysobjects where Type = 'U'
declare @iter int, @max int, @name varchar(200)
select @max = max(TableID) from #myTables
select @iter = 1
while @iter <= @max
begin
select @name = TableName from #myTables where TableID = @iter
exec ('delete from ' + @name)
select @iter = @iter + 1
end
drop table #myTables
 
Similar to my solution (thanks Kolt saved me typing) but use TRUNCATE TABLE instead of delete from - much faster.

If its a sql script I would put a USE MyDB at the start just to be safe.
 
Also look at the earlier, almost identical post thread183-534784 lots more info there.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top