Home Microsoft SQL Server DigiMailing iRN Contact
    Keyword



sys.sp_cdc_create_change_enumeration_functions

  No additional text.


Syntax
create procedure sys.sp_cdc_create_change_enumeration_functions
(
	@source_schema sysname,
	@source_name sysname,
	@capture_instance sysname,
	@supports_net_changes bit
)	
as
begin
	declare @retcode int
		,@column sysname
		,@stmt nvarchar(max)
		,@column_list nvarchar(max)
		,@null_column_list nvarchar(max)
		,@column_list_outer nvarchar(max)
		,@pk_max_where_clause nvarchar(max)
		,@pk_top_where_clause nvarchar(max)
		,@pk_group_clause nvarchar(max)
		,@has_access_function nvarchar(1000)
		,@has_access_function_net_changes nvarchar(1000)
		,@has_access_function_false nvarchar(1000)
		,@has_access_function_net_changes_false nvarchar(1000)
		,@net_changes_function nvarchar(1000)
		,@all_changes_function nvarchar(1000)
		,@quoted_change_table nvarchar(1000)
		,@quoted_column nvarchar(1000)
		,@quoted_source_table nvarchar(1000)
		,@min_op sysname
		,@count sysname
		,@newid sysname
		,@max_seqval sysname
		
    set nocount on

    -- Use first 8 characters of uniqueidentifier to qualify column names internal to the TVF
    -- that could conflict with user column names.
    set @newid = SUBSTRING(CONVERT(nvarchar(36), NEWID()),1,8)
    set @min_op = N'__$min_op_' + @newid
    set @count = N'__$count_' + @newid
    set @max_seqval = N'__$max_seqval_' + @newid

	create table #index_column_names(column_name sysname, index_ordinal int)

	set @quoted_source_table  = quotename(@source_schema) + N'.' + quotename(@source_name)
    set @quoted_change_table  = N'[cdc].' + quotename(@capture_instance + N'_CT')
    set @net_changes_function = N'[cdc].' + quotename(N'fn_cdc_get_net_changes' + N'_' + @capture_instance )
    set @all_changes_function = N'[cdc].' + quotename(N'fn_cdc_get_all_changes' + N'_' + @capture_instance )

    set @has_access_function  =
		N'( [sys].[fn_cdc_check_parameters]( N''' + replace(rtrim(@capture_instance), N'''', N'''''') + N''', @from_lsn, @to_lsn, lower(rtrim(ltrim(@row_filter_option))), 0) = 1)'
    set @has_access_function_net_changes  =
		N'( [sys].[fn_cdc_check_parameters]( N''' + replace(rtrim(@capture_instance), N'''', N'''''') + N''', @from_lsn, @to_lsn, lower(rtrim(ltrim(@row_filter_option))), 1) = 1)'
    set @has_access_function_false  =
		N'( [sys].[fn_cdc_check_parameters]( N''' + replace(rtrim(@capture_instance), N'''', N'''''') + N''', @from_lsn, @to_lsn, lower(rtrim(ltrim(@row_filter_option))), 0) = 0)'
    set @has_access_function_net_changes_false  =
		N'( [sys].[fn_cdc_check_parameters]( N''' + replace(rtrim(@capture_instance), N'''', N'''''') + N''', @from_lsn, @to_lsn, lower(rtrim(ltrim(@row_filter_option))), 1) = 0)'

	insert into #index_column_names
	select column_name, index_ordinal
	from [cdc].[index_columns]
	where object_id = object_id(@quoted_change_table)

	declare #hcolumns cursor local fast_forward for
		select name
		from [sys].[columns]
		where object_id = object_id(@quoted_change_table)
		and name not in
			(N'__$start_lsn', N'__$end_lsn', N'__$seqval', N'__$operation', N'__$update_mask')
		order by column_id
	
	open #hcolumns
	fetch #hcolumns into @column
	set @column_list = N''
	set @null_column_list = N''
	set @column_list_outer = N''
	
	while (@@fetch_status <> -1)
	begin
		set @column_list = @column_list + N', t.' + quotename(@column)
		set @null_column_list = @null_column_list + N', NULL as ' + quotename(@column)
		set @column_list_outer = @column_list_outer + N', ' + quotename(@column) 	
		fetch #hcolumns into @column
	end
	
	close #hcolumns
	deallocate #hcolumns
	
	set @stmt =
	N'
	create function ' + @all_changes_function + N'
	(	@from_lsn binary(10),
		@to_lsn binary(10),
		@row_filter_option nvarchar(30)
	)
	returns table
	return
	
	select NULL as __$start_lsn,
		NULL as __$seqval,
		NULL as __$operation,
		NULL as __$update_mask' +
	       @null_column_list + N'
	where ' + @has_access_function_false + N'

	union all
	
	select t.__$start_lsn as __$start_lsn,
		t.__$seqval as __$seqval,
		t.__$operation as __$operation,
		t.__$update_mask as __$update_mask' +
	       @column_list + N'
	from ' + @quoted_change_table + N' t with (nolock)
	where (lower(rtrim(ltrim(@row_filter_option))) = ''all'')
	    and ' + @has_access_function + N'
		and (t.__$operation = 1 or t.__$operation = 2 or t.__$operation = 4)
		and (t.__$start_lsn <= @to_lsn)
		and (t.__$start_lsn >= @from_lsn)
		
	union all	
		
	select t.__$start_lsn as __$start_lsn,
		t.__$seqval as __$seqval,
		t.__$operation as __$operation,
		t.__$update_mask as __$update_mask' +
	       @column_list + N'
	from ' + @quoted_change_table + N' t with (nolock)
	where (lower(rtrim(ltrim(@row_filter_option))) = ''all update old'')
	    and ' + @has_access_function + N'
		and (t.__$operation = 1 or t.__$operation = 2 or t.__$operation = 4 or
		     t.__$operation = 3 )
		and (t.__$start_lsn <= @to_lsn)
		and (t.__$start_lsn >= @from_lsn)
	'	
		
	exec (@stmt)
	if (@@error != 0)
	begin
		raiserror(22958, 16, -1, @capture_instance)
		return 1
	end

	if (@supports_net_changes != 1)
	begin
		return 0
	end	
	
	declare #hidxcolumns cursor local fast_forward for
		select column_name
		from #index_column_names
		order by index_ordinal
	
	open #hidxcolumns
	fetch #hidxcolumns into @column
	
	set @pk_max_where_clause = N' ( '
	set @pk_top_where_clause = N' ( '
	set @pk_group_clause      = N' '
	
	if (@@fetch_status <> -1)
	begin
    	set @quoted_column = quotename(@column)

		set @pk_max_where_clause  = N' ( (t.' + @quoted_column + N' = m.' + @quoted_column + N')'
		set @pk_top_where_clause  = N' ( (c.' + @quoted_column + N' = t.' + @quoted_column + N')'
		set @pk_group_clause      = N' r.' + @quoted_column

		fetch #hidxcolumns into @column
	end
	
	while (@@fetch_status <> -1)
	begin
		set @quoted_column = quotename(@column)

		set @pk_max_where_clause  = @pk_max_where_clause  + N' and (t.' + @quoted_column + N' = m.' + @quoted_column + N') '
		set @pk_top_where_clause  = @pk_top_where_clause  + N' and (c.' + @quoted_column + N' = t.' + @quoted_column + N') '
		set @pk_group_clause      = @pk_group_clause      + N', r.' + @quoted_column
		
		fetch #hidxcolumns into @column
	end
	
	set @pk_max_where_clause = @pk_max_where_clause + N' ) '
	set @pk_top_where_clause = @pk_top_where_clause + N' ) '
	
	close #hidxcolumns
	deallocate #hidxcolumns
	
	set @stmt =
	N'
	create function ' + @net_changes_function + N'
	(	@from_lsn binary(10),
		@to_lsn binary(10),
		@row_filter_option nvarchar(30)
	)
	returns table
	return

	select NULL as __$start_lsn,
		NULL as __$operation,
		NULL as __$update_mask' +
	       @null_column_list + N'
	where ' + @has_access_function_net_changes_false + N'

	union all
	
	select __$start_lsn,
	    case ' + @count + N'
	    when 1 then __$operation
	    else
			case ' + @min_op + N'
				when 2 then 2
				when 4 then
				case __$operation
					when 1 then 1
					else 4
					end
				else
				case __$operation
					when 2 then 4
					when 4 then 4
					else 1
					end
			end
		end as __$operation,
		null as __$update_mask ' + @column_list_outer + N'
	from
	(
		select t.__$start_lsn as __$start_lsn, __$operation,
		case ' + @count + N'
		when 1 then __$operation
		else
		(	select top 1 c.__$operation
			from ' + @quoted_change_table + N' c with (nolock)
			where ' + @pk_top_where_clause + N'
			and ((c.__$operation = 2) or (c.__$operation = 4) or (c.__$operation = 1))
			and (c.__$start_lsn <= @to_lsn)
			and (c.__$start_lsn >= @from_lsn)
			order by c.__$seqval) end ' + @min_op + N', ' + @count + @column_list + N'
		from ' + @quoted_change_table + N' t with (nolock) inner join
		(	select ' + @pk_group_clause + N', max(r.__$seqval) as ' + @max_seqval + N',
		    count(*) as ' + @count + N'
			from ' + @quoted_change_table + N' r with (nolock)
			where  (r.__$start_lsn <= @to_lsn)
			and (r.__$start_lsn >= @from_lsn)
			group by  ' + @pk_group_clause + N') m
		on t.__$seqval = m.' + @max_seqval + N' and
		   ' + @pk_max_where_clause + N'	
		where lower(rtrim(ltrim(@row_filter_option))) = N''all''
			and ' + @has_access_function_net_changes + N'
			and (t.__$start_lsn <= @to_lsn)
			and (t.__$start_lsn >= @from_lsn)
			and ((t.__$operation = 2) or (t.__$operation = 4) or
				 ((t.__$operation = 1) and
				  (2 not in
				 		(	select top 1 c.__$operation
							from ' + @quoted_change_table + N' c with (nolock)
							where ' + @pk_top_where_clause + N'
							and ((c.__$operation = 2) or (c.__$operation = 4) or (c.__$operation = 1))
							and (c.__$start_lsn <= @to_lsn)
							and (c.__$start_lsn >= @from_lsn)
							order by c.__$seqval
						 )
	 			   )
	 			 )
	 			) 	
	) Q
	
	union all
	
	select __$start_lsn,
	    case ' + @count + N'
	    when 1 then __$operation
	    else
			case ' + @min_op + N'
				when 2 then 2
				when 4 then
				case __$operation
					when 1 then 1
					else 4
					end
				else
				case __$operation
					when 2 then 4
					when 4 then 4
					else 1
					end
			end
		end as __$operation,
		case ' + @count + N'
		when 1 then
			case __$operation
			when 4 then __$update_mask
			else null
			end
		else	
			case ' + @min_op + N'
			when 2 then null
			else
				case __$operation
				when 1 then null
				else __$update_mask
				end
			end	
		end as __$update_mask ' + @column_list_outer + N'
	from
	(
		select t.__$start_lsn as __$start_lsn, __$operation,
		case ' + @count + N'
		when 1 then __$operation
		else
		(	select top 1 c.__$operation
			from ' + @quoted_change_table + N' c with (nolock)
			where ' + @pk_top_where_clause + N'
			and ((c.__$operation = 2) or (c.__$operation = 4) or (c.__$operation = 1))
			and (c.__$start_lsn <= @to_lsn)
			and (c.__$start_lsn >= @from_lsn)
			order by c.__$seqval) end ' + @min_op + N', ' + @count + N',
		m.__$update_mask ' + @column_list + N'
		from ' + @quoted_change_table + N' t with (nolock) inner join
		(	select ' + @pk_group_clause + N', max(r.__$seqval) as ' + @max_seqval + N',
		    count(*) as ' + @count + N',
		    [sys].[ORMask](r.__$update_mask) as __$update_mask
			from ' + @quoted_change_table + N' r with (nolock)
			where  (r.__$start_lsn <= @to_lsn)
			and (r.__$start_lsn >= @from_lsn)
			group by  ' + @pk_group_clause + N') m
		on t.__$seqval = m.' + @max_seqval + N' and
		   ' + @pk_max_where_clause + N'	
		where lower(rtrim(ltrim(@row_filter_option))) = N''all with mask''
			and ' + @has_access_function_net_changes + N'
			and (t.__$start_lsn <= @to_lsn)
			and (t.__$start_lsn >= @from_lsn)
			and ((t.__$operation = 2) or (t.__$operation = 4) or
				 ((t.__$operation = 1) and
				  (2 not in
				 		(	select top 1 c.__$operation
							from ' + @quoted_change_table + N' c with (nolock)
							where ' + @pk_top_where_clause + N'
							and ((c.__$operation = 2) or (c.__$operation = 4) or (c.__$operation = 1))
							and (c.__$start_lsn <= @to_lsn)
							and (c.__$start_lsn >= @from_lsn)
							order by c.__$seqval
						 )
	 			   )
	 			 )
	 			) 	
	) Q
	
	union all
	
		select t.__$start_lsn as __$start_lsn,
		case t.__$operation
			when 1 then 1
			else 5
		end as __$operation,
		null as __$update_mask ' + @column_list + N'
		from ' + @quoted_change_table + N' t  with (nolock) inner join
		(	select ' + @pk_group_clause + N', max(r.__$seqval) as ' + @max_seqval + N'
			from ' + @quoted_change_table + N' r with (nolock)
			where  (r.__$start_lsn <= @to_lsn)
			and (r.__$start_lsn >= @from_lsn)
			group by  ' + @pk_group_clause + N') m
		on t.__$seqval = m.' + @max_seqval + N' and
		   ' + @pk_max_where_clause + N'	
		where lower(rtrim(ltrim(@row_filter_option))) = N''all with merge''
			and ' + @has_access_function_net_changes + N'
			and (t.__$start_lsn <= @to_lsn)
			and (t.__$start_lsn >= @from_lsn)
			and ((t.__$operation = 2) or (t.__$operation = 4) or
				 ((t.__$operation = 1) and
				   (2 not in
				 		(	select top 1 c.__$operation
							from ' + @quoted_change_table + N' c with (nolock)
							where ' + @pk_top_where_clause + N'
							and ((c.__$operation = 2) or (c.__$operation = 4) or (c.__$operation = 1))
							and (c.__$start_lsn <= @to_lsn)
							and (c.__$start_lsn >= @from_lsn)
							order by c.__$seqval
						 )
	 				)
	 			 )
	 			)
	 '			 	
		
	exec (@stmt)
	if (@@error != 0)
	begin
		raiserror(22959, 16, -1, @capture_instance)
		return 1
	end

	return 0
	
end

 
Last revision 2008RTM
See also

  sp_cdc_enable_table_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