I was pretty much following MSDN to get net changes for the CDC enabled table for given date; refer to my earlier question, but was not able to figure what the error was.
Here is sample script to re-produce the error (which I assume is some sort of bug in CDC SQL 2012).
Step1: Create Database
CREATE DATABASE [CDCTest] go
Step 2: Create tables
--Create required tables USE [CDCTest] GO CREATE TABLE [dbo].[Customer]( [CustomerId] [int] NOT NULL, [CustomerName] [nvarchar](50) NOT NULL, [Country] [nvarchar](50) NOT NULL, [Email] [nvarchar](150) NOT NULL, [DateModified] [date] NOT NULL, CONSTRAINT [PK_Customer] PRIMARY KEY CLUSTERED ( [CustomerId] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] CREATE TABLE [dbo].[Product]( [ProductId] [int] NOT NULL, [Name] [varchar](150) NOT NULL, [Country] [varchar](50) NOT NULL, [Cost Price] [decimal](18, 5) NOT NULL, [Sale Price] [decimal](18, 5) NOT NULL, [Modified Date] [date] NOT NULL, CONSTRAINT [PK_Product] PRIMARY KEY CLUSTERED ( [ProductId] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO
Step 3: Enable CDC on database and on required tables
use CDCTest Exec sp_changedbowner 'sa' exec sys.sp_cdc_disable_db Exec sys.sp_cdc_enable_db --Product-- exec sys.sp_cdc_enable_table @source_schema = N'dbo', @source_name = 'Product', @capture_instance = 'ProductChanges', @supports_net_changes = 1, @role_name = Null --Customer-- exec sys.sp_cdc_enable_table @source_schema = N'dbo', @source_name = 'Customer', @capture_instance = 'CustomerChanges', @supports_net_changes = 1, @role_name = Null Go
Step 4: Insert Data into tables
USE [CDCTest] GO INSERT INTO [dbo].[Product] ([ProductId] ,[Name] ,[Country] ,[Cost Price] ,[Sale Price] ,[Modified Date]) VALUES (101001,'HP Sanners','Korea',120.99, 199.99,getdate()), (101002,'HP LaserJet Printer','Singapore',600.99, 800.99,getdate()), (101003,'Logitech Wifi KB','Korea',60.99, 90.99,getdate()) INSERT INTO [dbo].[Customer] ([CustomerId] ,[CustomerName] ,[Country] ,[Email] ,[DateModified]) VALUES (101,'John Smith','Australia','jsmith@domain.com',getdate()), (102,'Warick Leitch','New Zealand','wleitch@domain.com',getdate()), (103,'Jessica Kate','Singapore','jkate@domain.com',getdate()) Go
Step 5: Get net changes
DECLARE @begin_time datetime, @end_time datetime, @begin_lsn binary(10), @end_lsn binary(10); select @begin_time = min(tran_begin_time) from [cdc].[lsn_time_mapping]; Select @end_time = max(tran_begin_time) from [cdc].[lsn_time_mapping]; SELECT @begin_lsn = sys.fn_cdc_map_time_to_lsn('smallest greater than', @begin_time); SELECT @end_lsn = sys.fn_cdc_map_time_to_lsn('largest less than or equal', @end_time); SELECT * FROM cdc.fn_cdc_get_net_changes_CustomerChanges(@begin_lsn, @end_lsn, 'all') SELECT * FROM cdc.fn_cdc_get_net_changes_ProductChanges(@begin_lsn, @end_lsn, 'all');
This works fine with only one table (the one which is first in Step 3).
If you change the order of CDC enabled on tables in Step, this always works with only one table which is the first in step 3.
Msg 313, Level 16, State 3, Line 8
An insufficient number of arguments were supplied for the procedure or function cdc.fn_cdc_get_net_changes_ ... .
Any clues?
Sree