Hi Friends ,
in Sql Server 2008r2
I have database (Employee ) having 1000 table i want to truncate all table at a time if it is possible through sql query or store dprocedure Please let me know
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Anupam SinghRank #643 · 3.7K reputationPosted May 21, 2014, 9:53 PM EDT
Try this trick :
select 'TRUNCATE TABLE '+name from sys.tables
where type_desc='USER_TABLE' // OR YOU CAN SPECIFY TABLE USING WILDCARD
Copy result and execute it on SSMS.
Hope it helps.
Abhay ShankerRank #375 · 6.7K reputationPosted May 21, 2014, 10:21 PM EDT
Declare @t varchar (1024)Declare tbl_cur cursor for select TABLE_NAME from INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE'OPEN tbl_curFETCH NEXT from tbl_cur INTO @tWHILE @@FETCH_STATUS = 0BEGINEXEC ('TRUNCATE TABLE '+ @t)FETCH NEXT from tbl_cur INTO @tENDCLOSE tbl_curDEALLOCATE tbl_Cur for more details check below urls http://www.databasejournal.com/scripts/article.php/1497671/Script-to-Truncate-All-Tables-in-a-Database.htm http://stackoverflow.com/questions/6028960/sql-truncate-database-how-to-truncate-all-tablesJignesh TrivediRank #16 · 50.3K reputationPosted May 21, 2014, 11:02 PM EDT
hi,
Truncate table may not work with foreign key constrain so you have to perform delete operation.
there is one undocumented feature of SQL server call sp_msforeachtable, using this SP you can perform for each loop on tables
try...
http://mssqltrek.com/tag/truncate-all-tables-in-a-sql-server-database/
hope this will help you.