我正在尝试捕获现有的 CDC 值并禁用分区截断并再次启用。我无法弄清楚如何在动态 SQL 中处理空值。
如果我直接传递@role_name =NULL,&@filegroup_name = NULL。它工作正常。
-- Enable cdc for that table
declare @cmd_cdc_enable nvarchar(max)
SET @cmd_cdc_enable =N'
exec sys.sp_cdc_enable_table
@source_schema = '''+@v_source_schema+''',
@source_name = '''+@v_source_table+''',
@capture_instance = '''+@v_capture_instance +''',
@supports_net_changes = 1,
@role_name =NULL,
@filegroup_name = NULL,
@allow_partition_switch = 1
示例代码:
GO
EXEC sys.sp_cdc_enable_db
GO
create schema [C1810429]
go
create table [C1810429].[STYTOTAL_RAW_NEW] (ID int identity primary key, name varchar(200))
-- Enable First time
go
EXEC sys.sp_cdc_enable_table
@source_schema = N'C1810429',
@source_name = N'STYTOTAL_RAW_NEW',
@capture_instance = 'C1810429_STYTOTAL_RAW_NEW',
@supports_net_changes = 1 ,
@role_name = NULL,
@filegroup_name = NULL,
@allow_partition_switch = 1
drop table if exists tbl_sp_cdc_help_change_data_capture
create table tbl_sp_cdc_help_change_data_capture (
source_schema sysname null, source_table sysname null, capture_instance sysname null, object_id int null, source_object_id int null,
start_lsn binary(10) null, end_lsn binary(10) null, supports_net_changes bit null,has_drop_pending bit null, role_name sysname null,
index_name sysname null, filegroup_name sysname null, create_date datetime null, index_column_list nvarchar(max) null, captured_column_list nvarchar(max) null
)
insert into tbl_sp_cdc_help_change_data_capture
Exec sys.sp_cdc_help_change_data_capture @source_schema='C1810429',@source_name='STYTOTAL_RAW_NEW'
-- select * from tbl_sp_cdc_help_change_data_capture
declare @v_source_schema sysname
declare @v_source_table sysname
declare @v_capture_instance sysname
declare @v_supports_net_changes int
declare @v_role_name sysname
declare @v_filegroup_name sysname
select @v_source_schema=source_schema,@v_source_table=source_table,@v_capture_instance=capture_instance,@v_supports_net_changes=supports_net_changes,
@v_role_name=role_name,@v_filegroup_name=[filegroup_name]
from tbl_sp_cdc_help_change_data_capture
--select @v_source_schema,@v_source_table,@v_capture_instance,@v_supports_net_changes,@v_role_name,@v_filegroup_name
/*
-- disable cdc for that table
declare @cmd_CDC_disable nvarchar(max)
SET @cmd_CDC_disable =N'
exec sys.sp_cdc_disable_table
@source_schema = '''+@v_source_schema+''',
@source_name = '''+@v_source_table+''',
@capture_instance = '''+@v_capture_instance+'''
'
--print @cmd_CDC_disable
EXEC sys.sp_executesql @cmd_CDC_disable
select 'Disabled'
*/
--select @v_source_schema,@v_source_table,@v_capture_instance,@v_supports_net_changes,@v_role_name,@v_filegroup_name
-- Enable cdc for that table
declare @cmd_cdc_enable nvarchar(max)
SET @cmd_cdc_enable =N'
exec sys.sp_cdc_enable_table
@source_schema = '''+@v_source_schema+''',
@source_name = '''+@v_source_table+''',
@capture_instance = '''+@v_capture_instance +''',
@supports_net_changes = 1,
@role_name ='''+@v_role_name +''',
@filegroup_name = '''+@v_filegroup_name +''',
@allow_partition_switch = 1
'
print @cmd_cdc_enable
EXEC sys.sp_executesql @cmd_cdc_enable
--select 'Enabled'