Drop Table

Support Forum for database administrators and web based access to important newsgroups related to databases
Register on Database Support Forum Edit your profileCalendarFind other Database Support forum membersFrequently Asked QuestionsSearch this forum -> 
For Database admins: Free Database-related Magazines Now Free shipping to Texas


Post New Thread










Thread
Author

Truncate table if exists
Hi,
I am trying to create a script that deletes transaction tables and leaves
master data like customer, vendors, inventory items, etc.  How can I use
TRUNCATE TABLE with an Exists?  My problem is I have 200+ tables, if I
simply use a list like:
truncate table01
truncate table02
truncate table03
...

I get errors if the table does not exist and have to manually run the
truncate statements.  Some tables may not exist if that part of the app is
never used.  I'm trying to make a list of all tables that could exist
without it erroring out 50+ times.

Thanks in advance.



Report this thread to moderator Post Follow-up to this message
Old Post
rdraider
01-26-06 09:57 PM


Re: Truncate table if exists
The following script may work for you::

--------------------------------------
IF  OBJECT_ID('table01')
 IS NOT NULL
TRUNCATE TABLE table1

IF  OBJECT_ID('table02')
  IS NOT NULL
TRUNCATE TABLE table2

...

IF OBJECT_ID('tableN') IS NOT NULL
TRUNCATE TABLE tableN
--------------------------------------

-- Damodar Periwal
Software Tree, Inc.
Simplify Data Integration
http://www.softwaretree.com


Report this thread to moderator Post Follow-up to this message
Old Post
dperiwal@softwaretree.com
01-26-06 09:57 PM


Re: Truncate table if exists
Checking for the object_id is indeed the way to go.

I once needed a quite similar thing and wrote this into a stored
procedure. Something like this:

/ ********************
 ********************
 ********************
***************
************
* Procedure: pr_TruncateTable
*
* Purpose:
* Truncates the table with the specified name. This is actually
nothing more
* than a truncate table which checks first if the table exists.
*
* Input: Table Name
*
* Examples:
* exec pr_TruncateTable 'table01'
* exec pr_TruncateTable 'dbo.table01'

 ********************
 ********************
 ********************
****************
***********/

create procedure pr_TruncateTable
(
@Table varchar(250)
)
as
begin
set nocount on

declare @SQL varchar(1500)

if exists ( select *
from [dbo].[sysobjects]
where [id] = object_id(@Table)
and objectproperty([id], N'IsUserTable') = 1 )
begin
set @SQL = 'truncate table ' + @Table
exec (@SQL)
end

set nocount off
end

go


Report this thread to moderator Post Follow-up to this message
Old Post
Mike
01-26-06 09:57 PM


Sponsored Links





Last Thread Next Thread
Post New Thread

Microsoft SQL Server forum archive

Show a Printable Version Email This Page to Someone! Receive updates to this thread
Microsoft SQL Server
Access database support
PostgreSQL Replication
SQL Server ODBC
FoxPro Support
PostgreSQL pgAdmin
SQL Server Clustering
MySQL ODBC
Web Applications with dBASE
SQL Server CE
MySQL++
Sybase Database Support
MS SQL Full Text Search
PostgreSQL Administration
SQL Anywhere support
DB2 UDB Database
Paradox Database Support
Filemaker Database
Berkley DB
SQL 2000/2000i database
ASE Database
Forum Jump:
All times are GMT. The time now is 04:19 AM.

 
Mobile devices forum | Database support forum archive




Copyrights DropTable.com Database Support Forum 2004 - 2006