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 2005 Forums
 Transact-SQL (2005)
 Improve this Truncate script please?

Author  Topic 

pjstreiff
Starting Member

12 Posts

Posted - 2007-10-26 : 15:07:18
Currently using this script to truncate all tables belonging to specified schema:

EXEC sp_MSforeachtable '
IF OBJECTPROPERTY(object_id(''?''), ''TableHasForeignRef'') = 1
DELETE FROM ?
else
TRUNCATE TABLE ?
'
GO

declare @RETURN_VALUE int
declare @command1 nvarchar(2000)
declare @whereand nvarchar(2000)
set @command1 = 'truncate table ?'
set @whereand = 'and uid = ''7''' -- arch schema = 7
use Database_name
exec @RETURN_VALUE = sp_MSforeachtable @command1=@command1, @whereand = @whereand

The only problem is that it requires that I find the ID of the schema which makes it manual and not very 'portable'. Not to mention, error prone.

Is there a way to do something similiar by passing a schema NAME parameter instead?

Thanks in advance,

Phil Streiff, MCDBA

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2007-10-26 : 15:11:52
Moving this out of the Script Library since it is not a fully functional script yet.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-10-29 : 03:29:42
Modify this as needed
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=65341

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -