ChangeTrackingCleanup.sql 704 B

12345678910111213141516171819
  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, object_name(object_id) as TableName
  4. into #CT_Tables
  5. from sys.change_tracking_tables
  6. -- Set up the variables
  7. declare @start int = 1, @end int = (select count(*) from #CT_Tables), @tablename varchar(255)
  8. while (@start <= @end)
  9. begin
  10. -- Fetch the table to be cleaned up
  11. select @tablename = TableName from #CT_Tables where TableID = @start
  12. -- Execute the manual cleanup stored procedure
  13. exec sp_flush_CT_internal_table_on_demand @tablename
  14. -- Increment the counter
  15. set @start = @start + 1
  16. end
  17. drop table #CT_Tables