首页>Database>source

我们正在尝试运行以下查询。

DECLARE @CounterNumber INT = 1
WHILE @CounterNumber <= 3
BEGIN 
    Declare @name varchar(255)
    Set @name = (Select Name from TableList where id = @CounterNumber)
    SELECT CHANGE_TRACKING_CURRENT_VERSION() as CurrentChangeTrackingVersion
    SELECT SYS_CHANGE_VERSION FROM [dbo].[table_store_ChangeTracking_version] WHERE TableName = @name
    
    DECLARE @sql NVARCHAR(MAX);
    DECLARE @currentVersion bigint  
    Set @currentVersion = (SELECT TOP 1 SYS_CHANGE_VERSION  FROM table_store_ChangeTracking_version WHERE [email protected])
    DECLARE @newVersion bigint = CHANGE_TRACKING_CURRENT_VERSION()
    SET @sql = 'SELECT
        Ct.sys_change_Operation,
        CT.sys_change_version,
        CT.id as ct_id,
        @name.* 
        FROM @name
        RIGHT OUTER JOIN CHANGETABLE(CHANGES [email protected], @currentVersion) CT ON @name.Id = CT.Id'
    SET @sql = REPLACE(@sql, '@name', quotename(@name))
    
    EXEC sp_executesql @sql
    set @CounterNumber = @CounterNumber + 1
END;

但是我们收到错误提示:

必须声明标量变量" @currentVersion"。

任何人都可以帮助我了解我们这里出了什么问题吗?

最新回答
  • 1月前
    1 #

    您需要正确地将参数传递给动态查询:
    EXEC sp_executesql @sql, N'@currentVersion bigint', @currentVersion = @currentVersion

  • optimization:通过用户名查询数据-对-最佳性能
  • Mysql:MySQL-删除后的Shink表