我们正在尝试运行以下查询。
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 TableName=@name)
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 dbo.@name, @currentVersion) CT ON @name.Id = CT.Id'
SET @sql = REPLACE(@sql, '@name', quotename(@name))
EXEC sp_executesql @sql
set @CounterNumber = @CounterNumber + 1
END;
但是我们收到错误说明:
必须声明标量变量“@currentVersion”。
谁能帮我理解我们这里出了什么问题?
您需要正确地将参数传递给您的动态查询:
EXEC sp_executesql @sql, N'@currentVersion bigint', @currentVersion = @currentVersion