Home Microsoft SQL Server DigiMailing iRN Contact
    Keyword



sys.sp_cdc_create_change_table

  No additional text.


Syntax
create procedure sys.sp_cdc_create_change_table
(
	@source_schema sysname,
	@source_name sysname,
	@capture_instance sysname,
	@filegroup_name sysname
)	
as
begin
	declare @retcode int
		,@stmt nvarchar(max)
		,@change_table nvarchar(1000)
		,@source_table nvarchar(1000)
		,@column_name sysname
		,@source_object_id int
		,@column_id int
		,@collation_name sysname
		,@filegroup_clause nvarchar(1000)
		,@column_count int
		,@is_sparse bit = 0
		,@is_column_set bit = 0
		
    set nocount on

    set @change_table = N'[cdc].' + quotename(@capture_instance + N'_CT')
    set @source_table = quotename(@source_schema) + N'.' + quotename(@source_name)
    set @source_object_id = object_id(@source_table)
    set @filegroup_clause = N'ON ' + quotename(ISNULL(@filegroup_name,N'default'))

   	declare #hcolumns cursor local fast_forward for
		select c.name, c.column_id, c.collation_name
				,c.is_sparse, c.is_column_set
		from [sys].[columns] c inner join #captured_columns i
			on c.name collate database_default = i.column_name collate database_default
		where c.object_id = @source_object_id
		order by column_id
		
    set @stmt = N'create table ' + @change_table +
		N'(
			[__$start_lsn]		binary(10) not null,				
			[__$end_lsn]		binary(10),					
			[__$seqval]			binary(10) not null,				
			[__$operation]		int not null,			
			[__$update_mask]	varbinary(128) '
						
	open #hcolumns
	fetch #hcolumns into @column_name
			,@column_id
			,@collation_name
			,@is_sparse
			,@is_column_set
			

	set @column_count = 0
	while (@@fetch_status <> -1)
	begin
		set @column_count = @column_count + 1	
		set @stmt = @stmt + N', ' + quotename(@column_name) +  N' ' +
			[sys].[fn_cdc_format_type]( @source_object_id
				,@column_id) +
			case (isnull(@collation_name,''))
				when N'' then N'' else N' COLLATE ' + @collation_name
			end

		if(@is_sparse = 1)
		begin
			set @stmt = @stmt + N' SPARSE '
		end
		if(@is_column_set = 1)
		begin
			raiserror(22854, 16, -1, @source_table)
			return 1
		end
		--set this bit on source table column to block updatetext/writetext operation, exposed by columnproperty(..,.., 'is_cdctracked')
		EXEC %%ColumnEx(ObjectID = @source_object_id, Name = @column_name).SetCDCTracked(Value = 1)
		fetch #hcolumns into @column_name
			,@column_id
			,@collation_name
			,@is_sparse
			,@is_column_set
	end
	
	close #hcolumns
	deallocate #hcolumns

	if(@column_count > 1019)
	begin
		raiserror(22925, 16, -1, @capture_instance, 1019)
		return 1
	end
	
	set @stmt = @stmt +  N') ' + @filegroup_clause
	
	exec (@stmt)
	if (@@error != 0)
	begin
		return 1
	end
	
    exec sp_MS_marksystemobject @change_table
    if (@@error != 0)
    begin
        return 1
    end

    return 0
end

 
Last revision 2008RTM
See also

  sp_cdc_create_change_table_index (Procedure)
sp_cdc_enable_table_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