我使用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列/变量。如果过程成功执行,将向用户通知该消息。如果有问题,我想向他们发送一条消息,位于我的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在结果集中调用存储过程时,是否有办法捕获此错误?
答案 0 :(得分: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
但是还要在CATCH
之后返回这些值,以便proc始终返回状态。
SELECT
@SUCCESS as success,
@MSG as message
这将为您提供用户友好的消息,以及可以根据需要记录的实际SQL错误(发生时)。