Truncate All Tables Records
Create Procedure truncateAllTables
@dbName nvarchar(512)
As
/*
It truncates all tables in specified database @dbName.
*/
Declare @sqlString nvarchar(4000);
Set @sqlString = '
Declare @t nvarchar (1024)
Declare tbl_cur cursor for ' +
'select TABLE_NAME from ' + @dbName + '.' + 'INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = ' + '''BASE TABLE'' ' +
'OPEN tbl_cur
FETCH NEXT from tbl_cur INTO @t ' +
'WHILE @@FETCH_STATUS = 0
BEGIN
EXEC (' + '''TRUNCATE TABLE ''' + '+''' + @dbName + '''+' + '''.dbo.'' + ' + '@t' + ') ' +
'FETCH NEXT from tbl_cur INTO @t ' +
'END
CLOSE tbl_cur
DEALLOCATE tbl_Cur';
--Print @sqlString;
Exec sp_executesql @sqlString;
GO