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_name sysname

傳回型別

binary(10)

備註

當擷取實例不存在或呼叫端無權存取與擷取實例相關聯的變更資料時,傳回0x00000000000000000000。

此函式通常用於識別與擷取實例相關聯的異動資料擷取時程表的低端點。 您也可以使用此函式來驗證查詢範圍的端點是否落在擷取實例時間軸內,再要求變更資料。 請務必執行這類檢查,因為在變更資料表上執行清除時,擷取實例的低端點會變更。 如果變更資料要求之間的時間相當長,即使是設定為先前變更資料要求之高端點的低端點,也可能位於目前的時間軸之外。

權限

需要系統管理員固定伺服器角色的成員資格,或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,而且可用來設定 @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)