为什么我不能在开始捕获块/ TSQL中分配一个值

时间:2016-02-16 09:38:45

标签: sql-server tsql

当我尝试为begin catch块中的变量赋值时。它说could not be evaluated

Error message

我想收集所有这些记录' ids并在流程结束时记录它们。

OPEN FilesCursor;
FETCH NEXT FROM FilesCursor INTO @recordId;
WHILE @@FETCH_STATUS = 0
BEGIN
    BEGIN DISTRIBUTED TRANSACTION;
        BEGIN TRY
            SET XACT_ABORT ON;
                SET @insertQuery = 'INSERT INTO '+ @remoteLinkedServerName +'.PatlatDB.dbo.TestTab...';

                SET @updateQuery = '...';

                EXECUTE(@insertQuery);
                EXECUTE(@updateQuery);
            SET XACT_ABORT OFF;
        END TRY
        BEGIN CATCH
            IF @@TRANCOUNT > 0
            BEGIN
                ROLLBACK TRANSACTION;
                SET @errorIds += ',' + CAST(@recordId AS VARCHAR);
                SET @errorMessage += '|' + CAST((SELECT ERROR_MESSAGE()) AS VARCHAR);
            END;
        END CATCH
        IF @@TRANCOUNT > 0
            COMMIT TRANSACTION;
FETCH NEXT FROM FilesCursor INTO @recordId;
END;
CLOSE FilesCursor;
DEALLOCATE FilesCursor;

0 个答案:

没有答案