Home Microsoft SQL Server DigiMailing iRN Contact
    Keyword



sys.sp_cdc_cleanup_change_tables

  No additional text.


Syntax
create procedure [sys].[sp_cdc_cleanup_change_tables]
(
	@threshold			bigint,
	@capture_instance	sysname
)
as
begin
    set nocount on
	
	declare @change_table nvarchar(1000)
		,@stmt nvarchar(max)
		,@retcode int
		,@raised_error int
		,@raised_message nvarchar(4000)
		,@db_name sysname
		,@delete_count bigint
		,@action nvarchar(1000)
		,@instance_name sysname
		,@low_water_mark binary(10)
		
	set @db_name = db_name()
	set @delete_count = 0	

    -- Determine whether all tables are to be cleaned up, or only
    -- those associated with a single capture instance
	declare #hchange_table cursor local fast_forward
	for
		select capture_instance, start_lsn
		from [cdc].[change_tables]
		where (@capture_instance is null) or (capture_instance = @capture_instance)
		
    open #hchange_table

    if (@@error != 0)
	begin
		return 1
	end	

    fetch #hchange_table into @instance_name, @low_water_mark

    set @change_table = N'[cdc].' +
		quotename(@instance_name + N'_CT')

    while (@@fetch_status<>-1)
    begin

		BEGIN TRY
		
			select @raised_error = 0, @delete_count = 1
	
			while (@low_water_mark is not null) and (@delete_count > 0)
			begin
	
				-- Delete up to @threshold qualifying rows while
				-- the number of deleted rows is positive
				set @stmt = N'delete top( @p1 ) ' +
					N' from ' + @change_table +
					N' where __$start_lsn < @p2 '
				exec sp_executesql @stmt = @stmt
					,@params = N'@p1 bigint, @p2 binary(10)'
					,@p1 = @threshold
					,@p2 = @low_water_mark
			
				set @delete_count = @@rowcount
					
			end
			
		END TRY
		
		BEGIN CATCH
		
			-- This is a best effort attempt to delete entries
			set @raised_error = ERROR_NUMBER()
			set @raised_message = ERROR_MESSAGE()
			
		END CATCH
		
		-- If an error was raised, return an informational message
		-- and keep going.
		if (@raised_error <> 0)
		begin
			raiserror(22852, 10, -1, @db_name, @stmt, @raised_error, @raised_message)
		end	

        fetch #hchange_table into @instance_name, @low_water_mark
        set @change_table = N'[cdc].' +
			quotename(@instance_name + N'_CT')

    end

    close #hchange_table
    deallocate #hchange_table

    select @raised_error = 0, @delete_count = 1

	-- Delete all entres in [cdc].[lsn_time_mapping] with @start_lsn less
	-- than the smallest current low water mark of the capture instances.
	BEGIN TRY
	
		set @action = N'select @low_water_mark = min(start_lsn) [cdc].[change_tables]'
		select @low_water_mark = min(start_lsn) from [cdc].[change_tables]

		set @action = N'delete top (@threshold) from [cdc].[lsn_time_mapping] where start_lsn < @low_water_mark'
		if (@low_water_mark is not null)
		begin
			while (@delete_count > 0)
			begin
	
				-- Delete up to @threshold qualifying rows while
				-- the number of deleted rows is positive
				delete top (@threshold) from [cdc].[lsn_time_mapping]
				where start_lsn < @low_water_mark
			
				set @delete_count = @@rowcount
				
			end
		end
	
		
	END TRY
	
	BEGIN CATCH
	
		-- This is a best effort attempt to delete obsolete entries in cdc.lsn_time_mapping
		set @raised_error = ERROR_NUMBER()
		set @raised_message = ERROR_MESSAGE()
		
	END CATCH
	
	-- If an error was raised, return an informational message and keep going.
	if (@raised_error <> 0)
	begin
		raiserror(22853, 10, -1, @db_name, @action, @raised_error, @raised_message)
	end	

    return 0

end

 
Last revision 2008RTM
See also

  sp_cdc_cleanup_change_table_internal (Procedure)
sp_cdc_cleanup_job_internal (Procedure)
sp_MSdrop_cdc (Procedure)
       



News

  Query a named instance
Sybase+ASE+silent+install
SQL 2008 R2
Deprecated procedures in SQL2008
Reporting Services item-level role definitions
Create all your missing indexes
Converting datetime field
Start MSSQL Server Profiler at time
Replicating MSSQL Server views
Exploring Microsoft Sharepoint
The OLE DB provider "SQLNCLI10" for linked server indicates.
Mobile solar charger
Oracle to SQL Server replication
Cannot insert the value NULL into column
Undocumented Microsoft SQL Server 2008
VMware
Zoekmachine optimalisatie
SQL Servers hidden objects
FckEditor
Flash in Ajax
De hype die AJAX heet
Barcode scannen
MySQL; Gratis, makkelijk en snel!
PHP
sIFR; de combinatie tussen HTML en Flash