Skip to content

Instantly share code, notes, and snippets.

@yodiz
Created March 25, 2025 09:28
Show Gist options
  • Save yodiz/8796b5cad0ff833ea69cfff86510b3a6 to your computer and use it in GitHub Desktop.
Save yodiz/8796b5cad0ff833ea69cfff86510b3a6 to your computer and use it in GitHub Desktop.
-- DROP TABLE Customer
-- EXEC sys.sp_cdc_disable_db
--https://learn.microsoft.com/en-us/sql/relational-databases/track-changes/enable-and-disable-change-data-capture-sql-server?view=sql-server-ver16
CREATE TAble Customer (
CustomerId INT,
Name VARCHAR(100),
CONSTRAINT PK_Customer PRIMARY KEY (CustomerId)
)
EXEC sys.sp_cdc_enable_db
EXEC sys.sp_cdc_enable_table
@source_schema = N'dbo',
@source_name = N'Customer',
@role_name = NULL,
@capture_instance = N'dbo_Customer',
@supports_net_changes = 0
INSERT INTO Customer (CustomerId, Name) VALUES (1, 'Micke')
INSERT INTO Customer (CustomerId, Name) VALUES (2, 'Johanna')
UPDATE Customer Set Name = 'Micke 3' WHERE CustomerId = 1
INSERT INTO Customer (CustomerId, Name) VALUES (3, 'Gurka')
--DECLARE @from_lsn AS BINARY (10), @to_lsn AS BINARY (10);
--SET @from_lsn = sys.fn_cdc_get_min_lsn('dbo_customer');
--SET @to_lsn = sys.fn_cdc_get_max_lsn();
--SELECT @from_lsn, @to_lsn
--SELECT * FROM cdc.fn_cdc_get_all_changes_dbo_Customer(@from_lsn, @to_lsn, N'all')
GO -- Kör det nedan separat
SELECT * FROM cdc.fn_cdc_get_all_changes_dbo_Customer(sys.fn_cdc_get_min_lsn('dbo_customer'), sys.fn_cdc_get_max_lsn(), N'all')
SELECT * FROM cdc.dbo_Customer_CT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment