sys.fn_cdc_get_min_lsn (Transact-SQL)

适用于:SQL Server

从cdc.change_tables系统表中返回指定捕获实例 的start_lsn 列值。 该值表示捕获实例的有效性间隔的低端点。

Transact-SQL 语法约定

语法

  
sys.fn_cdc_get_min_lsn ( 'capture_instance_name' )  

参数

capture_instance_name
捕获实例的名称。 capture_instance_namesysname

返回类型

binary(10)

注解

当捕获实例不存在或调用方未获得访问与该捕获实例关联的更改数据的授权时,将返回 0x00000000000000000000。

该函数通常用于标识与某个捕获实例关联的变更数据捕获时间线的低端点。 您也可以在请求更改数据之前,使用该函数来验证某个查询范围的端点是否处于捕获实例时间线内。 执行这种检查是很重要的,因为当对更改表执行清理时,捕获实例的低端点会发生变化。 如果更改数据的两次请求之间相距的时间很长,则即使将上一个更改数据请求的高端点设置为低端点,此低端点也可能处于在当前时间线之外。

权限

要求具有 sysadmin 固定服务器角色或 db_owner 固定数据库角色的成员身份。 对于所有其他用户,要求对源表中的所有已捕获列具有 SELECT 权限;如果已定义捕获实例的访问控制角色,则还要求具有该数据库角色的成员身份。

示例

A. 为指定捕获实例返回最小 LSN 值

以下示例返回 AdventureWorks2022 数据库中捕获实例 HumanResources_Employee 的最小 LSN 值。

USE AdventureWorks2-12;  
GO  
SELECT sys.fn_cdc_get_min_lsn ('HumanResources_Employee')AS min_lsn;  
  

B. 验证查询范围的低端点

下例使用 sys.fn_cdc_get_min_lsn 返回的最小 LSN 值验证某个更改数据查询的建议低端点对于捕获实例 HumanResources_Employee 的当前时间线是否有效。 本例假定已保存该捕获实例的上一个高端点 LSN,且此高端点 LSN 可用于设置 @save_to_lsn 变量。 出于本例的目的,将 @save_to_lsn 设置为 0x000000000000000000 以强制运行错误处理部分。

USE AdventureWorks2022;  
GO  
DECLARE @min_lsn binary(10), @from_lsn binary(10),@save_to_lsn binary(10), @to_lsn binary(10);  
-- Sets @save_to_lsn to the previous high endpoint saved from the last change data request.  
SET @save_to_lsn = 0x000000000000000000;  
-- Sets the upper endpoint for the query range to the current maximum LSN.  
SET @to_lsn = sys.fn_cdc_get_max_lsn();  
-- Sets the @min_lsn parameter to the current minimum LSN for the capture instance.  
SET @min_lsn = sys.fn_cdc_get_min_lsn ('HumanResources_Employee');  
-- Sets the low endpoint for the query range to the LSN that follows the previous high endpoint.  
SET @from_lsn = sys.fn_cdc_increment_lsn(@save_to_lsn);  
-- Tests to verify the low endpoint is valid for the current capture instance.  
IF (@from_lsn < @min_lsn)  
    BEGIN  
        RAISERROR('Low endpoint of the request interval is invalid.', 16, -1);  
    END  
ELSE  
-- Return the changes occurring within the query range.  
    SELECT * FROM cdc.fn_cdc_get_all_changes_HumanResources_Employee(@from_lsn, @to_lsn, 'all');  
GO  

另请参阅

sys.fn_cdc_get_max_lsn (Transact-SQL)
事务日志 (SQL Server)