Skip to content

Commit 80fd9e0

Browse files
authored
Refresh cdc.fn_cdc_get_all_changes_<capture_instance> article (UUF 363040) (#32896)
1 parent 2ca976e commit 80fd9e0

1 file changed

Lines changed: 111 additions & 93 deletions

File tree

Lines changed: 111 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
---
2-
title: "cdc.fn_cdc_get_all_changes_&lt;capture_instance&gt; (Transact-SQL)"
3-
description: "cdc.fn_cdc_get_all_changes_&lt;capture_instance&gt; (Transact-SQL)"
2+
title: "cdc.fn_cdc_get_all_changes_&lt;capture_instance&gt; (Transact-SQL)"
3+
description: "cdc.fn_cdc_get_all_changes_&lt;capture_instance&gt; Returns one row for each change applied to the source table within the specified log sequence number (LSN) range."
44
author: rwestMSFT
55
ms.author: randolphwest
6-
ms.date: "09/29/2021"
6+
ms.date: 01/21/2025
77
ms.service: sql
88
ms.subservice: system-objects
99
ms.topic: "reference"
@@ -14,95 +14,113 @@ helpviewer_keywords:
1414
dev_langs:
1515
- "TSQL"
1616
---
17-
# cdc.fn_cdc_get_all_changes_&lt;capture_instance&gt; (Transact-SQL)
17+
# cdc.fn_cdc_get_all_changes_&lt;capture_instance&gt; (Transact-SQL)
18+
1819
[!INCLUDE [SQL Server](../../includes/applies-to-version/sqlserver.md)]
1920

20-
Returns one row for each change applied to the source table within the specified log sequence number (LSN) range. If a source row had multiple changes during the interval, each change is represented in the returned result set. In addition to returning the change data, four metadata columns provide the information you need to apply the changes to another data source. Row filtering options govern the content of the metadata columns as well as the rows returned in the result set. When the 'all' row filter option is specified, each change has exactly one row to identify the change. When the 'all update old' option is specified, update operations are represented as two rows: one containing the values of the captured columns before the update and another containing the values of the captured columns after the update.
21-
22-
This enumeration function is created at the time that a source table is enabled for change data capture. The function name is derived and uses the format `cdc.fn_cdc_get_all_changes_<capture_instance>` where *capture_instance* is the value specified for the capture instance when the source table is enabled for change data capture.
23-
24-
:::image type="icon" source="../../includes/media/topic-link-icon.svg" border="false"::: [Transact-SQL syntax conventions](../../t-sql/language-elements/transact-sql-syntax-conventions-transact-sql.md)
25-
26-
## Syntax
27-
28-
```syntaxsql
29-
30-
cdc.fn_cdc_get_all_changes_capture_instance ( from_lsn , to_lsn , '<row_filter_option>' )
31-
32-
<row_filter_option> ::=
33-
{ all
34-
| all update old
35-
}
36-
```
37-
38-
## Arguments
39-
40-
#### *from_lsn*
41-
The LSN value that represents the low endpoint of the LSN range to include in the result set. *from_lsn* is **binary(10)**.
42-
43-
Only rows in the [cdc.&#91;capture_instance&#93;_CT](../../relational-databases/system-tables/cdc-capture-instance-ct-transact-sql.md) change table with a value in **__$start_lsn** greater than or equal to *from_lsn* are included in the result set.
44-
45-
#### *to_lsn*
46-
The LSN value that represents the high endpoint of the LSN range to include in the result set. *to_lsn* is **binary(10)**.
47-
48-
Only rows in the [cdc.&#91;capture_instance&#93;_CT](../../relational-databases/system-tables/cdc-capture-instance-ct-transact-sql.md) change table with a value in **__$start_lsn** greater than or equal to *from_lsn* and less than or equal to *to_lsn* are included in the result set.
49-
50-
#### <row_filter_option> ::= { all | all update old }
51-
An option that governs the content of the metadata columns as well as the rows returned in the result set.
52-
53-
Can be one of the following options:
54-
55-
all
56-
Returns all changes within the specified LSN range. For changes due to an update operation, this option only returns the row containing the new values after the update is applied.
57-
58-
all update old
59-
Returns all changes within the specified LSN range. For changes due to an update operation, this option returns both the row containing the column values before the update and the row containing the column values after the update.
60-
61-
## Table returned
62-
63-
|Column name|Data type|Description|
64-
|-----------------|---------------|-----------------|
65-
|**__$start_lsn**|**binary(10)**|Commit LSN associated with the change that preserves the commit order of the change. Changes committed in the same transaction share the same commit LSN value.|
66-
|**__$seqval**|**binary(10)**|Sequence value used to order changes to a row within a transaction.|
67-
|**__$operation**|**int**|Identifies the data manipulation language (DML) operation needed to apply the row of change data to the target data source. Can be one of the following:<br /><br /> 1 = delete<br /><br /> 2 = insert<br /><br /> 3 = update (captured column values are those before the update operation). This value applies only when the row filter option 'all update old' is specified.<br /><br /> 4 = update (captured column values are those after the update operation)|
68-
|**__$update_mask**|**varbinary(128)**|A bit mask with a bit corresponding to each captured column identified for the capture instance. This value has all defined bits set to 1 when **__$operation** = 1 or 2. When **__$operation** = 3 or 4, only those bits corresponding to columns that changed are set to 1.|
69-
|**\<captured source table columns>**|varies|The remaining columns returned by the function are the captured columns identified when the capture instance was created. If no columns were specified in the captured column list, all columns in the source table are returned.|
70-
71-
## Permissions
72-
Requires membership in the **sysadmin** fixed server role or **db_owner** fixed database role. For all other users, requires SELECT permission on all captured columns in the source table and, if a gating role for the capture instance was defined, membership in that database role. When the caller does not have permission to view the source data, the function returns error 229 `The SELECT permission was denied on the object 'fn_cdc_get_all_changes_...', database '\<DatabaseName>', schema 'cdc'.`
73-
74-
## Remarks
75-
76-
Columns of data type **image**, **text**, and **ntext** are always assigned a NULL value when **__$operation** = 1 or **__$operation** = 3. Columns of data type **varbinary(max)**, **varchar(max)**, or **nvarchar(max)** are assigned a NULL value when **__$operation** = 3 unless the column changed during the update. When **__$operation** = 1, these columns are assigned their value at the time of the delete. Computed columns that are included in a capture instance always have a value of NULL.
77-
78-
Error 313 is expected if LSN range supplied is not appropriate when calling `cdc.fn_cdc_get_all_changes_<capture_instance>` or `cdc.fn_cdc_get_net_changes_<capture_instance>`. If the `lsn_value` parameter is beyond the time of lowest LSN or highest LSN, then execution of these functions will return in error 313: `Msg 313, Level 16, State 3, Line 1 An insufficient number of arguments were supplied for the procedure or function`. This error should be handled by the developer.
79-
80-
## Examples
81-
Several [!INCLUDE[ssManStudioFull](../../includes/ssmanstudiofull-md.md)] templates are available that show how to use the change data capture query functions. These templates are available on the **View** menu in [!INCLUDE[ssManStudio](../../includes/ssmanstudio-md.md)]. For more information, see [Template Explorer](../../ssms/template/template-explorer.md).
82-
83-
This example shows the `Enumerate All Changes for Valid Range Template`. It uses the function `cdc.fn_cdc_get_all_changes_HR_Department` to report all the currently available changes for the capture instance `HR_Department`, which is defined for the source table HumanResources.Department in the [!INCLUDE[ssSampleDBobject](../../includes/sssampledbobject-md.md)] database.
84-
85-
```sql
86-
-- ========
87-
-- Enumerate All Changes for Valid Range Template
88-
-- ========
89-
USE AdventureWorks2022;
90-
GO
91-
92-
DECLARE @from_lsn binary(10), @to_lsn binary(10);
93-
SET @from_lsn = sys.fn_cdc_get_min_lsn('HR_Department');
94-
SET @to_lsn = sys.fn_cdc_get_max_lsn();
95-
SELECT * FROM cdc.fn_cdc_get_all_changes_HR_Department
96-
(@from_lsn, @to_lsn, N'all');
97-
GO
98-
```
99-
100-
## See Also
101-
- [cdc.fn_cdc_get_net_changes_&#60;capture_instance&#62; &#40;Transact-SQL&#41;](../../relational-databases/system-functions/cdc-fn-cdc-get-net-changes-capture-instance-transact-sql.md)
102-
- [sys.fn_cdc_map_time_to_lsn &#40;Transact-SQL&#41;](../../relational-databases/system-functions/sys-fn-cdc-map-time-to-lsn-transact-sql.md)
103-
- [sys.sp_cdc_get_ddl_history &#40;Transact-SQL&#41;](../../relational-databases/system-stored-procedures/sys-sp-cdc-get-ddl-history-transact-sql.md)
104-
- [sys.sp_cdc_get_captured_columns &#40;Transact-SQL&#41;](../../relational-databases/system-stored-procedures/sys-sp-cdc-get-captured-columns-transact-sql.md)
105-
- [sys.sp_cdc_help_change_data_capture &#40;Transact-SQL&#41;](../../relational-databases/system-stored-procedures/sys-sp-cdc-help-change-data-capture-transact-sql.md)
106-
- [About Change Data Capture &#40;SQL Server&#41;](../../relational-databases/track-changes/about-change-data-capture-sql-server.md)
107-
108-
21+
Returns one row for each change applied to the source table within the specified log sequence number (LSN) range. If a source row had multiple changes during the interval, each change is represented in the returned result set. In addition to returning the change data, four metadata columns provide the information you need to apply the changes to another data source.
22+
23+
Row filtering options govern the content of the metadata columns and the rows returned in the result set. When the **all** row filter option is specified, each change has exactly one row to identify the change. When the **all update old** option is specified, update operations are represented as two rows: one containing the values of the captured columns before the update and another containing the values of the captured columns after the update.
24+
25+
This enumeration function is created at the time that a source table is enabled for change data capture. The function name is derived and uses the format `cdc.fn_cdc_get_all_changes_<capture_instance>` where *capture_instance* is the value specified for the capture instance when the source table is enabled for change data capture.
26+
27+
:::image type="icon" source="../../includes/media/topic-link-icon.svg" border="false"::: [Transact-SQL syntax conventions](../../t-sql/language-elements/transact-sql-syntax-conventions-transact-sql.md)
28+
29+
## Syntax
30+
31+
```syntaxsql
32+
cdc.fn_cdc_get_all_changes_<capture_instance> ( from_lsn , to_lsn , '<row_filter_option>' )
33+
34+
<row_filter_option> ::=
35+
{ all
36+
| all update old
37+
}
38+
```
39+
40+
## Arguments
41+
42+
#### *from_lsn*
43+
44+
The LSN value that represents the low endpoint of the LSN range to include in the result set. *from_lsn* is **binary(10)**.
45+
46+
Only rows in the [cdc.&#91;capture_instance&#93;_CT](../../relational-databases/system-tables/cdc-capture-instance-ct-transact-sql.md) change table with a value in `__$start_lsn` greater than or equal to *from_lsn* are included in the result set.
47+
48+
#### *to_lsn*
49+
50+
The LSN value that represents the high endpoint of the LSN range to include in the result set. *to_lsn* is **binary(10)**.
51+
52+
Only rows in the [cdc.&#91;capture_instance&#93;_CT](../../relational-databases/system-tables/cdc-capture-instance-ct-transact-sql.md) change table with a value in `__$start_lsn` greater than or equal to *from_lsn* and less than or equal to *to_lsn* are included in the result set.
53+
54+
#### <row_filter_option>
55+
56+
An option that governs the content of the metadata columns and the rows returned in the result set.
57+
58+
Can be one of the following options:
59+
60+
- **all**
61+
62+
Returns all changes within the specified LSN range. For changes due to an update operation, this option only returns the row containing the new values after the update is applied.
63+
64+
- **all update old**
65+
66+
Returns all changes within the specified LSN range. For changes due to an update operation, this option returns both the row containing the column values before the update and the row containing the column values after the update.
67+
68+
## Table returned
69+
70+
| Column name | Data type | Description |
71+
| --- | --- | --- |
72+
| `__$start_lsn` | **binary(10)** | Commit LSN associated with the change that preserves the commit order of the change. Changes committed in the same transaction share the same commit LSN value. |
73+
| `__$seqval` | **binary(10)** | Sequence value used to order changes to a row within a transaction. |
74+
| `__$operation` | **int** | Identifies the data manipulation language (DML) operation needed to apply the row of change data to the target data source. Can be one of the following values:<br /><br />`1` = delete<br />`2` = insert<br />`3` = update (captured column values are column values before the update operation). This value applies only when the row filter option 'all update old' is specified.<br />`4` = update (captured column values are column values after the update operation) |
75+
| `__$update_mask` | **varbinary(128)** | A bit mask with a bit corresponding to each captured column identified for the capture instance. This value has all defined bits set to `1` when `__$operation` is `1` or `2`. When `__$operation` is `3` or `4`, only those bits corresponding to columns that changed are set to `1`. |
76+
| `<captured source table columns>` | varies | The remaining columns returned by the function are the captured columns identified when the capture instance was created. If no columns were specified in the captured column list, all columns in the source table are returned. |
77+
78+
## Permissions
79+
80+
Requires membership in the **sysadmin** fixed server role or **db_owner** fixed database role. For all other users, requires SELECT permission on all captured columns in the source table and, if a gating role for the capture instance was defined, membership in that database role. When the caller doesn't have permission to view the source data, the function returns error 229:
81+
82+
```output
83+
The SELECT permission was denied on the object 'fn_cdc_get_all_changes_...', database '<DatabaseName>', schema 'cdc'.
84+
```
85+
86+
## Remarks
87+
88+
Columns of data type **image**, **text**, and **ntext** are always assigned a `NULL` value when `__$operation` is `1` or `__$operation` is `3`. Columns of data type **varbinary(max)**, **varchar(max)**, or **nvarchar(max)** are assigned a `NULL` value when `__$operation` is `3` unless the column changed during the update. When `__$operation` is `1`, these columns are assigned their value at the time of the delete. Computed columns that are included in a capture instance always have a value of `NULL`.
89+
90+
Error 313 is expected if the LSN range supplied isn't appropriate when calling `cdc.fn_cdc_get_all_changes_<capture_instance>` or `cdc.fn_cdc_get_net_changes_<capture_instance>`. If the `lsn_value` parameter is beyond the time of lowest LSN or highest LSN, then execution of these functions returns error 313:
91+
92+
```output
93+
Msg 313, Level 16, State 3, Line 1
94+
An insufficient number of arguments were supplied for the procedure or function.
95+
```
96+
97+
This error should be handled by the developer.
98+
99+
## Examples
100+
101+
Several [!INCLUDE [ssManStudioFull](../../includes/ssmanstudiofull-md.md)] templates are available that show how to use the change data capture query functions. These templates are available on the **View** menu in [!INCLUDE [ssManStudio](../../includes/ssmanstudio-md.md)]. For more information, see [Template Explorer](../../ssms/template/template-explorer.md).
102+
103+
This example shows the `Enumerate All Changes for Valid Range Template`. It uses the function `cdc.fn_cdc_get_all_changes_HR_Department` to report all the currently available changes for the capture instance `HR_Department`, which is defined for the source table `HumanResources.Department` in the [!INCLUDE [ssSampleDBobject](../../includes/sssampledbobject-md.md)] database.
104+
105+
```sql
106+
-- Enumerate All Changes for Valid Range Template
107+
USE AdventureWorks2022;
108+
GO
109+
110+
DECLARE @from_lsn AS BINARY (10), @to_lsn AS BINARY (10);
111+
SET @from_lsn = sys.fn_cdc_get_min_lsn('HR_Department');
112+
SET @to_lsn = sys.fn_cdc_get_max_lsn();
113+
114+
SELECT *
115+
FROM cdc.fn_cdc_get_all_changes_HR_Department(@from_lsn, @to_lsn, N'all');
116+
GO
117+
```
118+
119+
## Related content
120+
121+
- [cdc.fn_cdc_get_net_changes_&lt;capture_instance&gt; (Transact-SQL)](cdc-fn-cdc-get-net-changes-capture-instance-transact-sql.md)
122+
- [sys.fn_cdc_map_time_to_lsn (Transact-SQL)](sys-fn-cdc-map-time-to-lsn-transact-sql.md)
123+
- [sys.sp_cdc_get_ddl_history (Transact-SQL)](../system-stored-procedures/sys-sp-cdc-get-ddl-history-transact-sql.md)
124+
- [sys.sp_cdc_get_captured_columns (Transact-SQL)](../system-stored-procedures/sys-sp-cdc-get-captured-columns-transact-sql.md)
125+
- [sys.sp_cdc_help_change_data_capture (Transact-SQL)](../system-stored-procedures/sys-sp-cdc-help-change-data-capture-transact-sql.md)
126+
- [What is change data capture (CDC)?](../track-changes/about-change-data-capture-sql-server.md)

0 commit comments

Comments
 (0)