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)
 loop statement/truncate table statement

Author  Topic 

dnaes
Starting Member

3 Posts

Posted - 2007-12-15 : 21:33:29
i have multiple tables that we have created for our data warehouse that begin with 'rej'. these are the reject tables and need to be truncated before our etl process begins. i have written a loop statement but cannot pass the variable @rej_nm to the truncate table statement. here's my logic.

1> create table variable @rej_tbl(nm, identity column)
2> load all tables from sysobjects that begin with 'rej' into @rej_tbl
3> create/load @rej_nm from @rej_tbl (first table name that needs to be truncated)
4> loop thru and truncate each @rej_nm value. CANNOT get statement to work:
truncate table @rej_nm

please advise..thanks y'all!!

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2007-12-15 : 21:41:57
use dynamic sql.

_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
SSMS Add-in that does a few things: www.ssmstoolspack.com <- new version out
Go to Top of Page

jezemine
Master Smack Fu Yak Hacker

2886 Posts

Posted - 2007-12-15 : 23:08:28
exec('truncate table ' + quotename(@rej_nm))


elsasoft.org
Go to Top of Page

dnaes
Starting Member

3 Posts

Posted - 2007-12-16 : 08:56:36
exec('truncate table ' + quotename(@rej_nm))

worked great...thank you!
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-12-17 : 03:56:42
and if you want to truncate all tables then use http://sqlteam.com/forums/topic.asp?TOPIC_ID=65341

Madhivanan

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

- Advertisement -