Sre*_*har 5 sql-server cdc change-data-capture sql-server-2012
我试图通过传递最小和最大日期来获得启用CDC的表上的净更改.但是丢错误.
Msg 313, Level 16, State 3, Line 24
An insufficient number of arguments were supplied for the procedure or function cdc.fn_cdc_get_net_changes_ ... .
Run Code Online (Sandbox Code Playgroud)
我的代码如下:
DECLARE @CDate DATE = '2013-03-18' --This is the date after the CDC was enabled on the table
DECLARE @count INT;
DECLARE @lsnStartDatetime DATETIME;
DECLARE @lsnEndDateTime DATETIME;
DECLARE @begin_time DATETIME ,
@end_time DATETIME ,
@from_lsn BINARY(10) ,
@to_lsn BINARY(10);
SELECT @lsnStartDatetime = CAST(CAST(@CDate AS NVARCHAR(10)) + ' 00:00:00' AS DATETIME)
SELECT @lsnEndDateTime = CAST(CAST(@CDate AS NVARCHAR(10)) + ' 23:59:59' AS DATETIME)
SET @from_lsn = sys.fn_cdc_map_time_to_lsn('smallest greater than or equal',
@lsnStartDatetime);
SET @to_lsn = sys.fn_cdc_map_time_to_lsn('largest less than or equal',
@lsnEndDateTime);
if exists (select * from sys.objects where name = 'EmployeeCDCbyDate' and type = 'u')
drop table etl.EmployeeCDCbyDate
SELECT *
FROM cdc.fn_cdc_get_net_changes_employee(@from_lsn, @to_lsn, N'all')
Run Code Online (Sandbox Code Playgroud)
从sys.fn_cdc_map_time_to_lsn获取的from_lsn和to_lsn是否与cdc表'employee'映射的不匹配
下面的代码工作正常; 但它从min max lsn获得所有净变化.
DECLARE @min_lsn BINARY(10) = sys.fn_cdc_get_min_lsn ('employee')
DECLARE @max_lsn BINARY(10) = sys.fn_cdc_get_max_lsn ()
SELECT * FROM cdc.fn_cdc_get_net_changes_employee(@min_lsn, @max_lsn, 'all') ORDER BY 1 desc
Run Code Online (Sandbox Code Playgroud)
我需要的是获得给定日期的cdc实例的min和max lsn并获得该日期的净更改.有线索吗?
编辑:
当我启用一堆表时,这适用于第一个表.
例如:
USE ERP
EXEC sys.sp_cdc_disable_db
EXEC sys.sp_cdc_enable_db
EXEC sys.sp_cdc_enable_table @source_schema = N'dbo',
@source_name = N'Employee',
@capture_instance = 'Employee',
@supports_net_changes =1,
@role_name = NULL
EXEC sys.sp_cdc_enable_table @source_schema = N'dbo',
@source_name = N'StoreListing',
@capture_instance = 'StoreListing',
@supports_net_changes =1,
@role_name = NULL
Go
Run Code Online (Sandbox Code Playgroud)
这适用于Employee表.如果我改变了他们启用CDC的顺序(如果我先将商店列表放在第一位,员工接下来),那么它适用于员工列表.
MSDN Social 上有一个类似问题的答案: Is there bug with cdc.fn_cdc_get_net_changes_.... in SQL Server 2012。
它被标记为由主持人回答。这是引用:
我在我的服务器上测试过。当我在SQL Server 2008 R2中运行该脚本时,它可以成功运行,没有错误。
当我在 SQL Server 2012 中运行该脚本时,出现的错误消息与您的错误消息相同。我想你可以尝试应用最新的SQL Server 2012 Service Pack看看是否可以。
这是 Microsoft 的错误报告。目前正在调查中。