Home Microsoft SQL Server DigiMailing iRN Contact
    Keyword



sys.sp_cdc_create_populate_stored_procs

  No additional text.


Syntax
create procedure sys.sp_cdc_create_populate_stored_procs
(
	@capture_instance sysname,
	@ddl_operation sysname = N'create'
)	
as
begin
	declare @column sysname
		,@column_id int
		,@is_computed bit
		,@stmt nvarchar(max)
		,@old nvarchar(4)
		,@new nvarchar(4)
		,@quoted_change_table nvarchar(1000)
		,@change_table_object_id int
		,@insdel_proc nvarchar(1000)
		,@upd_proc nvarchar(1000)
		,@batchinsert_proc nvarchar(1000)
		,@column_type_list nvarchar(max)
		,@column_type_list_old nvarchar(max)
		,@column_type_list_new nvarchar(max)
		,@column_type_list_batch nvarchar(max)
		,@column_list nvarchar(max)
		,@column_list_withrow1 nvarchar(max)
		,@column_list_batch nvarchar(max)
		,@column_list_select nvarchar(max)
		,@column_parameter_list nvarchar(max)
		,@column_parameter_list_old nvarchar(max)
		,@column_parameter_list_new nvarchar(max)
		,@column_parameter_list_batch nvarchar(max)
		,@column_type_list_size int
		,@newid sysname
		,@rownum sysname
		
    set nocount on

    -- Use first 8 characters of uniqueidentifier to qualify column name internal to the sp
    -- that could conflict with a user column name.
    set @newid = SUBSTRING(CONVERT(nvarchar(36), NEWID()),1,8)
    set @rownum = N'__$rownum' + @newid

    -- We will insure parameter is either 'create' or 'alter'
    set @ddl_operation = lower(rtrim(ltrim(@ddl_operation)))
    if (@ddl_operation <> N'alter')
    begin
		set @ddl_operation = N'create'
	end	

    set @old = N'_old'
    set @new = N'_new'
    set @quoted_change_table  = N'[cdc].' + quotename(@capture_instance + N'_CT')
   	set @change_table_object_id = object_id(@quoted_change_table)

    set @insdel_proc = N'[cdc].' + quotename(N'sp_insdel_' + convert(nvarchar(10),@change_table_object_id))
    set @upd_proc    = N'[cdc].' + quotename(N'sp_upd_'    + convert(nvarchar(10),@change_table_object_id))
    set @batchinsert_proc    = N'[cdc].' + quotename(N'sp_batchinsert_'    + convert(nvarchar(10),@change_table_object_id))

	declare #hcolumns cursor local fast_forward for
		select c.column_name, s.column_id, c.is_computed
		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_object_id
		order by s.column_id
	
	open #hcolumns
	fetch #hcolumns into @column
			,@column_id
			,@is_computed
	set @column_type_list = N''
	set @column_type_list_old = N''
	set @column_type_list_new = N''
	set @column_type_list_batch = N''
	set @column_list = N''
	set @column_list_withrow1 = N''
	set @column_list_select = N''
	set @column_parameter_list = N''
	set @column_parameter_list_old = N''
	set @column_parameter_list_new = N''
	set @column_parameter_list_batch = N''
	set @column_type_list_size = 0

	while (@@fetch_status <> -1)
	begin
		set @column_list = @column_list + N', ' + quotename(@column)
		if (@is_computed = 0)
		begin
			set @column_type_list = @column_type_list + N', @c' + convert(nvarchar(10),@column_id) +  N' ' +
				[sys].[fn_cdc_format_type]( @change_table_object_id
					,@column_id)
			set @column_type_list_old = @column_type_list_old + N', @c' + convert(nvarchar(10),@column_id) + @old +  N' ' +
				[sys].[fn_cdc_format_type]( @change_table_object_id
					,@column_id)
			set @column_type_list_new = @column_type_list_new + N', @c' + convert(nvarchar(10),@column_id) + @new +  N' ' +
				[sys].[fn_cdc_format_type]( @change_table_object_id
					,@column_id)
			set @column_type_list_batch = @column_type_list_batch + N', @c' + convert(nvarchar(10),@column_id) +  N'_XXX ' +
				[sys].[fn_cdc_format_type]( @change_table_object_id, @column_id)

			set @column_parameter_list = @column_parameter_list + N', @c' + convert(nvarchar(10),@column_id)
			set @column_parameter_list_old = @column_parameter_list_old + N', @c' + convert(nvarchar(10),@column_id) + @old
			set @column_parameter_list_new = @column_parameter_list_new + N', @c' + convert(nvarchar(10),@column_id) + @new
			set @column_parameter_list_batch = @column_parameter_list_batch + N', @c' + convert(nvarchar(10),@column_id) + N'_XXX'
			set @column_list_withrow1 = @column_list_withrow1 + N', @c' + convert(nvarchar(10),@column_id) + N'_1 ' + quotename(@column)
			set @column_list_select = @column_list_select + N', ' + quotename(@column)

			set @column_type_list_size = @column_type_list_size + 1
		end
		else
		begin
			set @column_parameter_list = @column_parameter_list + N', null'
			set @column_parameter_list_old = @column_parameter_list_old + N', null'
			set @column_parameter_list_new = @column_parameter_list_new + N', null'
			set @column_list_select = @column_list_select + N', null'
		end
		
		fetch #hcolumns into @column
			,@column_id
			,@is_computed
	end
	
	close #hcolumns
	deallocate #hcolumns


	
	set @stmt = @ddl_operation +
	N'
	procedure ' + @insdel_proc + N'
	(	@__$start_lsn binary(10),
		@__$seqval binary(10),
		@__$operation int,
		@__$update_mask varbinary(128) ' + @column_type_list + N'
	)
	as
	begin
		insert into ' + @quoted_change_table + N'
		(
			__$start_lsn
			,__$end_lsn
			,__$seqval
			,__$operation
			,__$update_mask ' + @column_list + N'
		)
		values
		(
			@__$start_lsn
			,NULL
			,@__$seqval
			,@__$operation
			,@__$update_mask ' + @column_parameter_list + N'
		)
		return 0
	end														'
	
	exec (@stmt)

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

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

	set @stmt = @ddl_operation +
	N'
	procedure ' + @upd_proc + N'
	(	@__$start_lsn binary(10),
		@__$seqval binary(10),
		@__$update_mask varbinary(128) ' + @column_type_list_old  +  @column_type_list_new + N'
	)
	as
	begin
		insert into ' + @quoted_change_table + N'
		(
			__$start_lsn
			,__$end_lsn
			,__$seqval
			,__$operation
			,__$update_mask ' + @column_list + N'
		)
		values
		(
			@__$start_lsn
			,NULL
			,@__$seqval
			,3
			,@__$update_mask ' + @column_parameter_list_old + N'
		)
		
		insert into ' + @quoted_change_table + N'
		(
			__$start_lsn
			,__$end_lsn
			,__$seqval
			,__$operation
			,__$update_mask ' + @column_list + N'
		)
		values
		(
			@__$start_lsn
			,NULL
			,@__$seqval
			,4
			,@__$update_mask ' + @column_parameter_list_new + N'
		)
		
		return 0
	end														'

	exec (@stmt)

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

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

	--create the stored proc for batch populating
	declare @maxrowcount int
		    ,@currow int
		    ,@ministmt nvarchar(max)

	set @column_type_list_batch =
	N'
	  @__$start_lsn_XXX binary(10), @__$seqval_XXX binary(10), @__$operation_XXX int, @__$update_mask_XXX varbinary(128)' + @column_type_list_batch
	
	set @column_list_batch = N'__$start_lsn, __$end_lsn, __$seqval, __$operation, __$update_mask' + @column_list
	set @column_list_select = N'__$start_lsn, NULL, __$seqval, __$operation, __$update_mask' + @column_list_select
	set @column_list_withrow1 = N'@__$start_lsn_1 __$start_lsn, @__$seqval_1 __$seqval, @__$operation_1 __$operation, @__$update_mask_1 __$update_mask' + @column_list_withrow1
	set @column_parameter_list_batch = N'@__$start_lsn_XXX,  @__$seqval_XXX, @__$operation_XXX, @__$update_mask_XXX' + @column_parameter_list_batch
	set @maxrowcount = (2100 - 1)/(4 + @column_type_list_size); -- >= 2, < 524

	set @stmt = @ddl_operation +
	N'
	procedure ' + @batchinsert_proc + N'
	(
	 @rowcount int,'
	
	set @currow = 1;
	while @currow < @maxrowcount
	begin
		set @stmt = @stmt + REPLACE(@column_type_list_batch, N'XXX', convert(nvarchar(3), @currow)) + N','
		set @currow = @currow + 1
	end
	set @stmt = @stmt + REPLACE(@column_type_list_batch, N'XXX', convert(nvarchar(3), @currow)) +
	N'
	)														
	as
	begin
	   set nocount on	
	   insert into ' + @quoted_change_table + N' ('  + @column_list_batch + N')
	   select top(@rowcount) ' + @column_list_select + N'
	   from (
	             select 1 ' + @rownum + N',' + @column_list_withrow1
	
	set @ministmt  =
	N'
	             union all
	             select XXX, ' + @column_parameter_list_batch

	set @currow = 2;
       while @currow <= @maxrowcount
	begin
		set @stmt = @stmt + REPLACE(@ministmt, N'XXX', convert(nvarchar(3), @currow))
		set @currow = @currow + 1
	end	

	set @stmt = @stmt +
	N'
	           ) rowcollection
	    where ' + @rownum + N' <= @rowcount
	end'

	exec (@stmt)

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

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

 
Last revision 2008RTM
See also

  sp_cdc_enable_table_internal (Procedure)
sp_cdc_logddl_internal (Procedure)
sp_cdc_vupgrade (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