3

我有一个我写的脚本,它在特定行上给我一个 3930 错误。这是一个简单的更新语句,当我在脚本中运行它时它会失败,但是如果我从脚本中打印变量并手动运行它,则该行执行得很好。有人知道我如何追踪这个错误吗?

该行在游标中执行,并在此循环之前正确运行数百次。如果我让它处理所有记录或更新我的游标查询以仅选择这一记录,我会收到错误消息。

完整的错误信息是:

消息 3930,级别 16,状态 1,第 160 行
当前事务无法提交,并且无法支持写入日志文件的操作。回滚事务。

违规行是:

UPDATE [DBName].[dbo].[TableName] 
SET [XXX] = @XXX, [DateEdited] = GETDATE(), [EditedBy] = 'MyUser' 
WHERE [YYY] = @YYY

编辑:

我对查询进行了清理,以便可以将其发布在这里;见下文。有几点需要注意。

1) 对于承包商“ABCDEF”,[FactoryDB].[dbo].[AuditInformation] 中只有一条记录,因此该记录不需要光标 B。我尝试删除光标并保留所有包含的代码,但这修复了错误

2) 删除解析年龄的 try-catch 块也可以修复错误,即使剩余的任何代码都没有使用它。仅供参考,代码会出错并且 catch 块会执行。

--TABLE TO HOLD ERROR MESSAGES
declare @errors TABLE (FormId int, FactoryContractorCode nvarchar(50), FactoryId nvarchar(50), ErrorMessage nvarchar(1000) )

--DECLARE VARIABLES
DECLARE @factoryCode nvarchar(50)
DECLARE @factoryId nvarchar(50)
DECLARE @formId int
DECLARE @auditDate DateTime
DECLARE @totalEmployees int
DECLARE @ageRange nvarchar(50)
DECLARE @ageAverage int
DECLARE @percentageFemale int
DECLARE @percentageMale int
DECLARE @rowIdToUpdate nvarchar(50)

DECLARE @tempFormId int
DECLARE @tempDateString nvarchar(50)
DECLARE @tempAuditDate DateTime

--CURSOR TO LOOP THROUGH ALL FACTORY CODES
DECLARE a CURSOR FOR
    SELECT DISTINCT [FactoryCode] FROM [FactoryDB].[dbo].[AuditInformation] WHERE [FactoryCode] = 'ABCDEF'
OPEN a
FETCH a into @factoryCode
WHILE(@@FETCH_STATUS = 0)
BEGIN
BEGIN TRANSACTION

    --RESET VARIABLES TO NULL
    SET @totalEmployees = NULL
    SET @ageRange = NULL
    SET @formId = NULL
    SET @rowIdToUpdate = NULL
    SET @tempAuditDate = NULL
    SET @auditDate = NULL
    SET @ageAverage = NULL
    SET @percentageFemale = NULL
    SET @percentageMale = NULL
    SET @tempFormId = NULL
    SET @tempDateString = NULL
    SET @factoryId = NULL

    SELECT @factoryId=[ContractorID] FROM [DataDB].[dbo].[ContractorIdentifier] WHERE [ContractorCode] = @factoryCode

    --FIND THE FORM ID WITH THE MOST RECENT DATE
    DECLARE b CURSOR FOR
        SELECT [FormId],[StartDate] FROM [FactoryDB].[dbo].[AuditInformation] WHERE [FactoryCode] = @factoryCode AND NOT [StartDate] IS NULL
    OPEN b
    FETCH b into @tempFormId, @tempDateString
    WHILE(@@FETCH_STATUS = 0)
    BEGIN
        --PARSE THE FORM DATE
        BEGIN TRY
            SET @tempAuditDate = CAST(@tempDateString AS DateTime)
        END TRY
        BEGIN CATCH
            INSERT INTO @errors ([FormId],[FactoryContractorCode],[FactoryId],[ErrorMessage]) VALUES (@tempFormId, @factoryCode, @factoryId, 'Unable to parse date value of: '''+@tempDateString+'''')
        END CATCH

        --FIRST TIME
        IF (@auditDate IS NULL)
        BEGIN
            SET @auditDate = @tempAuditDate
            SET @formId = @tempFormId
        END

        --UPDATE IF THE NEW DATE IS MORE RECENT THAN THE PREVIOUS ONE
        IF(@tempAuditDate > @auditDate)
        BEGIN
            SET @auditDate = @tempAuditDate
            SET @formId = @tempFormId
        END

    FETCH b into @tempFormId, @tempDateString
    END
    CLOSE b
    DEALLOCATE b

    --IF A FORM WAS FOUND WITH A PARSABLE DATE
    IF (NOT @formId IS NULL)
    BEGIN
        --GET AUDIT INFORMATION
        SELECT TOP 1
            @totalEmployees = m.[TotarEmployees]
            ,@ageRange = m.[EmployeeAgeRange]
        FROM [FactoryDB].[dbo].[AuditInformation] g
        INNER JOIN [FactoryDB].[dbo].[AuditData] m on g._form_id = m._form_id
        WHERE g.[_form_id] = @formId

        --PARSE THE AVERAGE AGE FROM THE VARCHAR COLUMN
        BEGIN TRY
            DECLARE @low int
            DECLARE @high int

            SET @low = CAST(LTRIM(RTRIM(SUBSTRING(@ageRange,0,CHARINDEX('-',@ageRange)))) AS int)
            SET @high = CAST(LTRIM(RTRIM(SUBSTRING(@ageRange,CHARINDEX('-',@ageRange)+1,LEN(@ageRange)))) AS int)
            SET @ageAverage = ROUND((@low+@high)/2, 0)
        END TRY
        BEGIN CATCH
            INSERT INTO @errors ([FormId],[FactoryContractorCode],[FactoryId],[ErrorMessage]) VALUES (@formId, @factoryCode, @factoryId, 'Unable to parse age value of: '''+@ageRange+'''')
        END CATCH
    END


    --UPDATE THE FACTORY VIEW TABLE
    SELECT TOP 1 @rowIdToUpdate = [Id] FROM [DataDB].[dbo].[DataTable] WHERE [ContractorID] = @factoryId  AND [DateEdited] < @auditDate ORDER BY [DateEdited] DESC

    IF (NOT @rowIdToUpdate IS NULL)
    BEGIN

        IF(NOT @totalEmployees IS NULL)
            UPDATE [DataDB].[dbo].[EmployeeStats] SET  [TotalEmployees] = @totalEmployees, [DateEdited] = GETDATE(), [EditedBy] = 'LstAD' WHERE [RowIDs] = @rowIdToUpdate
        ELSE
            INSERT INTO @errors ([FormId],[FactoryContractorCode],[FactoryId],[ErrorMessage]) VALUES (@formId, @factoryCode, @factoryId, 'Did not update Total Employees. Value was null or unable to be calculated.')
    END

ROLLBACK
FETCH a into @factoryCode
END
CLOSE a
DEALLOCATE a

--DO SOMETHING WITH THE ERRORS TABLE VARIABLE
SELECT * FROM @errors
4

1 回答 1

3

事实证明,即使它在 Try/Catch 块中,错误也会阻止事务被提交。我不知道这一点。在尝试进行数学运算之前,我重新编写了“--PARSE THE AVERAGE AGE FROM THE VARCHAR COLUMN”代码以使用 ISNUMERIC 作为检查,从而解决了问题。感谢评论者的帮助。否则我不会想出来的。

于 2012-08-27T15:28:09.580 回答