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

相关文章:

  • 2021-07-02
  • 2022-12-23
  • 2020-02-16
  • 2021-08-09
  • 2022-01-18
  • 2021-11-10
  • 2021-02-01
  • 2021-05-16
猜你喜欢
  • 2022-03-03
  • 2022-12-23
  • 2021-07-29
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案