0

我使用 ColdFusion 调用存储过程来插入或更新用户数据。这两个事务在两个过程中分开。我的 SQL 代码应该返回行数 1 或 0 取决于事务是否成功。如果交易失败,我想知道在这种情况下处理错误的最佳方法是什么?两者cfstoredproc都包含在 try/catch 块中,但如果 SQL 过程中发生错误,我Count在结果集中的变量将返回 0,并且 try/catch 不会注册从过程返回的错误。这是我的代码示例:

<cftry>
    <cfif trim(arguments.process) EQ "Insert">
        <cfstoredproc procedure="InsertRec" datasource="#dsn#">
            <cfprocparam dbvarname="@Status" value="#trim(arguments.status)#" cfsqltype="cf_sql_bit" />
            <cfprocparam dbvarname="@Code" value="#trim(arguments.frmcode)#" cfsqltype="cf_sql_char" maxlength="2" null="#!len(trim(arguments.code))#" />
            <cfprocparam dbvarname="@Name" value="#trim(arguments.name)#" cfsqltype="cf_sql_varchar" maxlength="50" null="#!len(trim(arguments.name))#" />
            <cfprocresult name="Result"/>
        </cfstoredproc>
    <cfelse>
        <cfstoredproc procedure="UpdateRec" datasource="#dsn#">
            <cfprocparam dbvarname="@Status" value="#trim(arguments._status)#" cfsqltype="cf_sql_bit" />
            <cfprocparam dbvarname="@Code" value="#trim(arguments.code)#" cfsqltype="cf_sql_char" maxlength="2" null="#!len(trim(arguments.code))#" />
            <cfprocparam dbvarname="@Name" value="#trim(arguments.name)#" cfsqltype="cf_sql_varchar" maxlength="50" null="#!len(trim(arguments.name))#" />
            <cfprocresult name="Result"/>
        </cfstoredproc>
    </cfif>

    <cfset local.fnResults = {
         status : "200", 
         message : "Record successully saved!", 
         recCount : Result.Count
    }>

    <cfcatch type="any">
         <cfset local.fnResults = {
             error : cfcatch, <!--- I use this just for testing purpose. --->
             status : "400",
             message : "Error! Please contact your administrator."
         }>
    </cfcatch>
</cftry>

上面的代码返回 Count 列/变量,正如我在 Result set 中已经提到的那样。如果进程成功执行,用户将收到消息通知。如果有什么问题,我想给他们发一条在我的catch区块中的消息。这是SQL代码:

CREATE PROCEDURE [dbo].[InsertRec] 
    @Status BIT = NULL,
    @Name VARCHAR(50) = NULL,
    @Code CHAR(2) = NULL
AS
    SET NOCOUNT ON
    SET XACT_ABORT ON
    BEGIN TRY
        BEGIN
            INSERT INTO dbo.Books(
                Status,Name,Code
            )
            VALUES(
                @Status,@Name,@Code
            )
            SELECT @@ROWCOUNT AS Count;
        END
    END TRY
    BEGIN CATCH
        SELECT 
            @@ROWCOUNT AS Count, 
            ERROR_PROCEDURE() AS ErrorProcedure,
            ERROR_LINE() AS ErrorLine,
            ERROR_NUMBER() AS ErrorNumber,
            ERROR_MESSAGE() AS ErrorMessage,
            CURRENT_TIMESTAMP AS DateTime
    END CATCH

我只展示了插入 SQL 代码,因为更新过程是相同的。当我使用时,如果出现问题,我会看到此消息。当我试图插入已经存在的主键时,这只是一个例子:

COUNT   
0
DATETIME        
2018-08-24 07:00:01.58
ERRORLINE   
16
ERRORMESSAGE    
Violation of PRIMARY KEY constraint 'PK_Books'. Cannot insert duplicate key in object 'dbo.Books'. The duplicate key value is (44).
ERRORNUMBER 
2627    
ERRORPROCEDURE
InsertRec

ColdFusion 没有发现这个错误。当我使用 ColdFusion 在结果集中调用存储过程时,有没有办法捕捉到这个错误?

4

1 回答 1

4

正如@Ageax 所提到的,您正在处理存储过程中的错误,因此从CF 的角度来看,存储过程正确执行。

当存储过程不返回记录集时,我倾向于对所有对存储过程的调用使用标准消息包。将两个局部变量添加到您的 proc 并根据需要进行更新:

DECLARE @SUCCESS BIT = 1;
DECLARE @MSG VARCHAR(50) = 'Some default success message.'

CATCH在您的声明中更新这些值:

BEGIN CATCH
    SET @SUCCESS = 0;
    SET @MSG = 'There was a problem ...';
    SELECT 
        @SUCCESS as success,
        @MSG as message,
        @@ROWCOUNT AS Count, 
        ERROR_PROCEDURE() AS ErrorProcedure,
        ERROR_LINE() AS ErrorLine,
        ERROR_NUMBER() AS ErrorNumber,
        ERROR_MESSAGE() AS ErrorMessage,
        CURRENT_TIMESTAMP AS DateTime
END CATCH

但也要在 proc 之后返回这些值,CATCH以便 proc 始终返回一个状态。

SELECT 
    @SUCCESS as success,
    @MSG as message

这将为您提供用户友好的消息,以及可以根据需要记录的实际 SQL 错误(发生时)。

于 2018-08-29T00:01:24.657 回答