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 2008 Forums
 Transact-SQL (2008)
 truncate N tables in one swoop

Author  Topic 

yosiasz
Master Smack Fu Yak Hacker

1635 Posts

Posted - 2010-08-12 : 13:30:23
Hi there

I have a table that contains a list of staging tables names. staging tables could be added replace, removed etc. The process I have needs to truncate these staging tables. How can I do so in one take instead of looping through the metadata tables?

Thanks



If you don't have the passion to help people, you have no passion

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2010-08-12 : 13:42:53
declare @sql varchar(max)
select @sql=isnull(@sql,'') + replace('TRUNCATE TABLE [ ! ]','[ ! ]',table_name) +' '
from information_schema.tables where table_name --like 'staging%'


EXEC ( @sql)


Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

yosiasz
Master Smack Fu Yak Hacker

1635 Posts

Posted - 2010-08-12 : 13:51:42
gorgeous!

Thanks a lot Jim!

If you don't have the passion to help people, you have no passion
Go to Top of Page

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2010-08-12 : 14:10:35
You're welcome!

Everyday I learn something that somebody else already knew
Go to Top of Page
   

- Advertisement -