ChangeTrackingCleanup.sql 807 B

12345678910111213141516171819202122
  1. -- Loop to invoke manual cleanup procedure for cleaning up change tracking tables in a database
  2. -- Fetch the tables enabled for Change Tracking
  3. select identity(int, 1,1) as TableID, (SCHEMA_NAME(tbl.Schema_ID) +'.'+ object_name(ctt.object_id)) as TableName
  4. into #CT_Tables
  5. from sys.change_tracking_tables ctt
  6. INNER JOIN sys.tables tbl
  7. ON tbl.object_id = ctt.object_id
  8. -- Set up the variables
  9. declare @start int = 1, @end int = (select count(*) from #CT_Tables), @tablename varchar(255)
  10. while (@start <= @end)
  11. begin
  12. -- Fetch the table to be cleaned up
  13. select @tablename = TableName from #CT_Tables where TableID = @start
  14. -- Execute the manual cleanup stored procedure
  15. exec sp_flush_CT_internal_table_on_demand @tablename
  16. -- Increment the counter
  17. set @start = @start + 1
  18. end
  19. drop table #CT_Tables