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
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Truncating / deleting from all tables

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2002-04-18 : 09:49:55
Mark Janikiewicz writes "I have a database with approx 500 tables. The requirement is to delete all rows from every table.

Is there a quick method for doing this rather than manually running a delete / truncate on every table.

Or is it possible to produce a script to carry out

Version is SQL Server 7
Windows NT 4

Thanks in advance for any suggestions."

dsdeming

479 Posts

Posted - 2002-04-18 : 09:56:34
You could cursor through the INFORMATION_SCHEMA.TABLES view and dynamically issue a truncate table command for each user table.

Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2002-04-18 : 10:15:26
There is also an undocumented stored procedure called sp_MSforeachtable, you could use it like this:

EXECUTE sp_MSforeachtable 'TRUNCATE TABLE ?'

The question mark (?) is a placeholder for the table name. This procedure does exactly what dsdeming described; it cursors through all the table names and executes your command on each one.

If you want more information on this and other undocumented functions, pick up Ken Henderson's

The Guru's Guide to Transact-SQL

or

The Guru's Guide to SQL Server Stored Procedures, XML and HTML

Or pick up both! They're worth twice what you'll pay for them.

Go to Top of Page
   

- Advertisement -