sql-server - 更改数据捕获错误

标签 sql-server sql-server-2012 cdc change-data-capture

我试图通过传递最小和最大日期来获取启用 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_ ... .

我的代码如下:

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')

从 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

我需要的是获取给定日期的 cdc 实例的最小和最大 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

这适用于 Employee 表。如果我更改它们启用 CDC 的顺序(如果我将商店列表放在前面,然后将员工放在后面),那么它可以很好地处理员工列表。

最佳答案

MSDN Social 上有类似问题的答案:Is there bug with cdc.fn_cdc_get_net_changes_.... in SQL Server 2012 .

它被标记为由主持人回答。引用如下:

I tested in my server. When I ran the script in the SQL Server 2008 R2, it could be run successfully without error.

When I ran the script in SQL Server 2012, the error message came out the same as yours. I think you could try to apply the latest SQL Server 2012 Service Pack to see if it would OK.

And here is a bug report with Microsoft.正在调查中。

关于sql-server - 更改数据捕获错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15513808/

相关文章:

SQL 的 Fallthrough CASE 语句

sql - 为什么我无法使用 OBJECT_ID() 函数找到外键?

sql-server - 使用内连接 sql 查询进行计数

sql-server - StreamSets Data Collector 能否在目标数据库中自动创建表?

c - STM32 STM32CubeF4 USB CDC操作

sql - 如何在 SQL Server 2012 中将日期时间列拆分为日期列和串联时间(对于同一日期列)

sql-server - 列的数据类型不能参与列存储索引

sql-server - 为什么这些 SQL Server 统计信息在设置为自动更新时会过时?

sql-server - 为过程或函数cdc.fn_cdc_get_all_changes_提供的参数数量不足

sql-server - 如何查询特定用户访问权限