sys.fn_cdc_get_min_lsn (Transact-SQL)

cdc.change_tables 系統資料表傳回指定擷取執行個體的 start_lsn column 值。這個值代表擷取執行個體的有效性間隔低端點。

主題連結圖示Transact-SQL 語法慣例

語法

sys.fn_cdc_get_min_lsn ( 'capture_instance_name' )

引數

  • 'capture_instance_name'
    這是擷取執行個體的名稱。capture_instance_name 是 sysname。

傳回類型

binary(10)

備註

當擷取執行個體不存在,或者呼叫者未經授權,無法存取與擷取執行個體相關聯的變更資料時,便傳回 0x00000000000000000000。

這個函數通常是用於識別與擷取執行個體相關聯之異動資料擷取時間表的低端點。您也可以在要求變更資料之前,使用這個函數來驗證查詢範圍的端點是否都位於擷取執行個體時間表之內。請您務必執行這類檢查,因為在變更資料表上執行清除時,擷取執行個體的低端點就會變更。如果變更資料要求之間的時間很長,即使是上一次變更資料要求時設定為高端點的低端點也可能位於目前時間表以外。如需異動資料擷取時間表的詳細資訊,請參閱<設定異動資料擷取>。

權限

需要系統管理員 (sysadmin) 固定伺服器角色或 db_owner 固定資料庫角色中的成員資格。若為所有其他使用者,則需要來源資料表中所有擷取資料行的 SELECT 權限,而且如果定義了擷取執行個體的控制角色,便需要該資料庫角色的成員資格。

範例

A. 針對指定的擷取執行個體傳回最小 LSN 值

下列範例會針對 AdventureWorks 資料庫中的擷取執行個體 HumanResources_Employee 傳回最小 LSN 值。

USE AdventureWorks;
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 AdventureWorks;
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