Home Microsoft SQL Server DigiMailing iRN Contact
    Keyword



sys.sp_cdc_logddl_internal

  No additional text.


Syntax
create procedure sys.sp_cdc_logddl_internal
(
	@source_object_id int
	,@ddl_command nvarchar(max)
	,@ddl_lsn binary(10)
	,@ddl_time nvarchar(1000)
	,@commit_lsn binary(10)
	,@source_column_id int = null
	,@fis_alter_column bit = 0
	,@fis_drop_table bit = 0
)
AS
begin
	set nocount on
	declare @retcode int
		,@stmt nvarchar(max)
		,@change_table nvarchar(1000)
		,@source_table nvarchar(1000)
		,@change_table_id int
		,@required_column_update bit
		,@type_string_old nvarchar(1000)
		,@column_name sysname
		,@system_type_id int
		,@capture_instance sysname
		,@source_schema sysname
		,@source_name sysname
		,@column_id int
		,@type_string nvarchar(1000) = null
		,@db_name sysname

	-- If a source table has been dropped, we need to disable change tracking
	-- for the table
	if (@fis_drop_table = 1)
	begin
		-- Drop entries for a dropped source table in [cdc].[index_columns].
		delete from [cdc].[index_columns]
		where object_id in
			(
				select object_id from cdc.change_tables with (index = source_object_id_idx)
				where object_name(source_object_id) is null
			)	
		
		-- Drop entries for a dropped source table in [cdc].[captured_columns].
		delete from [cdc].[captured_columns]
		where object_id in
			(
				select object_id from cdc.change_tables with (index = source_object_id_idx)
				where object_name(source_object_id) is null
			)	

		-- Drop entries for a dropped source table in [cdc].[ddl_history].
		delete from [cdc].[ddl_history]
		where object_name(source_object_id) is null
		
   		declare #hinstance cursor local fast_forward
		for
			select capture_instance
			from cdc.change_tables with (index = source_object_id_idx)
			where object_name(source_object_id) is null

		-- Drop tracking entries for the individual capture instance
		open #hinstance
		fetch #hinstance into @capture_instance
	
		while (@@fetch_status <> -1)
		begin
			-- Drop cdc objects associated with the source table
			exec @retcode = [sys].[sp_cdc_drop_change_table_objects] @capture_instance
			
			fetch #hinstance into @capture_instance
		end	
		close #hinstance
		deallocate #hinstance

		-- Remove entries for source table in [cdc].[change_tables].
		delete [cdc].[change_tables]
		where object_name(source_object_id) is null
		
		return(0)
	
	end
	
	-- Make an entry in cdc.ddl_history for all capture instances associated with
	-- the tracked table.
	declare #hobjectids cursor local fast_forward for
		select object_id, capture_instance
		from  cdc.change_tables with (index = source_object_id_idx)
		where source_object_id = @source_object_id

	open #hobjectids
	fetch #hobjectids into @change_table_id, @capture_instance
	
	while (@@fetch_status <> -1)
	begin
		
		-- Determine for each instance whether a column update is needed.
		-- An update is only needed for a datatype change to a captured column
		-- that actually modifies the columns' associated type string.
		set @required_column_update = 0

		if @fis_alter_column = 1
		begin
		
			-- Determine the name and column id in the change table that corresponds
			-- to the column_id of the altered source column
			select @column_name = null, @column_id = null
			select @column_name = c.column_name, @column_id = s.column_id
			from cdc.captured_columns c inner join sys.columns s
				on c.object_id = s.object_id and
				   c.column_name = s.name
			where c.object_id = @change_table_id
			and   c.column_id = @source_column_id
		
			-- Determine the type string associated with the altered column if it is present
			-- in the change table.
			if (@column_name is not null) and (@column_id is not null)
			begin
				set @type_string_old = sys.fn_cdc_format_type(@change_table_id, @column_id)
				set @type_string = sys.fn_cdc_format_type(@source_object_id, @source_column_id)
				if @@error <> 0
					return (1)
				
				select @column_name = column_name
				from cdc.captured_columns
				where object_id = @change_table_id
				and   column_id = @source_column_id
				
				if (@type_string_old <> @type_string)
				begin
					set @required_column_update = 1
		
					set @stmt = N'alter table cdc.' + quotename(object_name(@change_table_id)) +
						N' alter column ' + quotename(@column_name) +
						N' ' + @type_string
					
					exec (@stmt)
				
					if (@@error <> 0)
					begin
						set @change_table = N'cdc.' + object_name(@change_table_id)
						raiserror(22976, 16, -1, @column_name, @change_table)
						return (1)
					end
				
					-- Update the column type in cdc.captured_columns
					set @system_type_id = null
				
					--type_name on clr UDT is null, use user type instead
					select @system_type_id = case system_type_id when 240 then user_type_id else system_type_id end
					from sys.columns
					where object_id = @change_table_id
					and name = @column_name
		
					if (@@error <> 0) or (@system_type_id is null)
					begin
						set @change_table = object_name(@change_table_id)
						raiserror(22978, 16, -1, @column_name, @change_table)
						return (1)
					end

					update cdc.captured_columns
					set column_type = type_name(@system_type_id)
					where object_id = @change_table_id
					and column_id = @source_column_id
				
					if @@error <> 0
					begin
						set @change_table = object_name(@change_table_id)
						raiserror(22978, 16, -1, @column_name, @change_table)
						return (1)
					end

					-- Alter the stored procedures to populate the change table
					exec @retcode = [sys].[sp_cdc_create_populate_stored_procs]
						@capture_instance, N'alter'
					if (@retcode != 0) or (@@error != 0)
					begin
						select @source_name = object_name(@source_object_id),
							@source_schema = object_schema_name(@source_object_id)
	 					raiserror(22982, 16, -1, @capture_instance, @source_schema, @source_name )
	 					return (1)
					end
				end
			end
		end		
	
		-- Update the history table.
		insert cdc.ddl_history (source_object_id, object_id, required_column_update,
			ddl_command, ddl_lsn, ddl_time)
		values (@source_object_id, @change_table_id, @required_column_update,
			@ddl_command, @ddl_lsn, @ddl_time)
		
		if @@error <> 0	
		begin
			set @change_table = object_name(@change_table_id)
			raiserror(22977, 16, -1, @change_table)
			return (1)
		end
		
		fetch #hobjectids into @change_table_id, @capture_instance
	end
	
	close #hobjectids
	deallocate #hobjectids
	
	return 0
end

 
Last revision 2008RTM
See also

  sp_MScdc_logddl (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