从CATCH块

时间:2018-08-23 18:21:40

标签: sql-server sql-server-2008 stored-procedures error-handling

我有一个存储过程,该过程执行带有try / catch块的简单插入事务。

如果交易成功,OUTPUT将返回插入的记录ID。如果发生错误,我想从try / catch块返回内容。在发生错误的情况下,我当前的代码将返回空白RecID。这是我的代码示例:

CREATE PROCEDURE [dbo].[InsertRecord] 
    @Status BIT = NULL,
    @Name VARCHAR(50) = NULL,
    @Code CHAR(2) = NULL,
    @ActionID UNIQUEIDENTIFIER = NULL
AS
    SET NOCOUNT ON
    SET XACT_ABORT ON
    BEGIN TRY
        BEGIN
            INSERT INTO dbo.Dictionary(
                Status,Name,Code,ActionDt,ActionID
            )
            OUTPUT INSERTED.RecID
            VALUES(
                @Status,@Name,@Code,CURRENT_TIMESTAMP,@ActionID
            );
        END
    END TRY
    BEGIN CATCH 
        SELECT  
            ERROR_PROCEDURE() AS ErrorProcedure,
            ERROR_LINE() AS ErrorLine,
            ERROR_NUMBER() AS ErrorNumber,
            ERROR_MESSAGE() AS ErrorMessage,
            CURRENT_TIMESTAMP AS DateTime
    END CATCH

如果我尝试插入已存在的记录,则上面的代码将返回以下内容:

ErrorProcedure  
InsertRecord    
ErrorLine   
16              
ErrorNumber 
2627
ErrorMessage    
Violation of PRIMARY KEY constraint 'PK_Code'.  
Cannot insert duplicate key in object 'dbo.Dictionary'.  
The duplicate key value is (44).
DateTime
2018-08-23 10:46:02.920

这是我所期望的,并显示在Management Studio 2008中。当我使用ColdFusion调用此过程时,我只会得到RecID = ''。这是我如何调用此过程的示例:

<cftry>
    <cfstoredproc procedure="InsertRecord" datasource="#dsn#">
        <cfprocparam dbvarname="@Status" value="#trim(arguments.frm_status)#" cfsqltype="cf_sql_bit" />
        <cfprocparam dbvarname="@Code" value="#trim(arguments.frm_code)#" cfsqltype="cf_sql_char" maxlength="2" null="#!len(trim(arguments.frm_code))#" />
        <cfprocparam dbvarname="@Name" value="#trim(arguments.frm_name)#" cfsqltype="cf_sql_varchar" maxlength="50" null="#!len(trim(arguments.frm_name))#" />
        <cfprocparam dbvarname="@ActionID" value="#trim(SESSION.UserID)#" cfsqltype="cf_sql_idstamp" maxlength="50" null="#!len(trim(SESSION.UserID))#" />
        <cfprocresult name="Result"/>
    </cfstoredproc>

    <cfset local.fnResults = {status : "200", RecID : Result.RecID}>

    <cfcatch type="any">
        <cfset local.fnResults = {error:cfcatch,status : "400", class : "alert-danger", message : "Error! Please contact your administrator."}>
    </cfcatch>
</cftry>

执行后是否有办法将存储过程中的错误返回给ColdFusion?

1 个答案:

答案 0 :(得分:1)

尝试cfcatch.detail以获得这些错误结果:

提前致歉。我确实没有进行测试/编译(但是我想您就明白了)以及有关如何处理错误的CF文档,从而做到了这一点。看看cfcatch.detail会带来什么:

cftry>
  <cfstoredproc procedure="InsertRecord" datasource="#dsn#">
        <cfprocparam dbvarname="@Status" value="#trim(arguments.frm_status)#" cfsqltype="cf_sql_bit" />
        <cfprocparam dbvarname="@Code" value="#trim(arguments.frm_code)#" cfsqltype="cf_sql_char" maxlength="2" null="#!len(trim(arguments.frm_code))#" />
        <cfprocparam dbvarname="@Name" value="#trim(arguments.frm_name)#" cfsqltype="cf_sql_varchar" maxlength="50" null="#!len(trim(arguments.frm_name))#" />
        <cfprocparam dbvarname="@ActionID" value="#trim(SESSION.UserID)#" cfsqltype="cf_sql_idstamp" maxlength="50" null="#!len(trim(SESSION.UserID))#" />
        <cfprocresult name="Result"/>
    </cfstoredproc>

    <cfset local.fnResults = {status : "200", RecID : Result.RecID}>

<cfcatch type="database">
   <cfoutput>
   <p><b>database error</b><br />
   <ul>
   <li><b>message:</b> #cfcatch.message#
   <li><b>native error code:</b> #cfcatch.nativeerrorcode#
   <li><b>sqlstate:</b> #cfcatch.sqlstate#
   <li><b>detail:</b> #cfcatch.detail#
   </ul>
   </cfoutput>
</cfcatch>
</cftry>