views:

276

answers:

2

Hi there,

I have a SQL stored procedure which under some situations will return a result of -1 if it fails, but also returns a message via the RAISERROR command e.g.:

BEGIN

    RAISERROR ('Error %i has occurred', 11, 1, 0)
    RETURN -1

END

I am accessing this via coldfusion using cfstoredproc e.g.:

<cfstoredproc procedure="sp_return" datasource="myDatasource" returncode="yes">
<cfdump var="#cfstoredproc#">

But the structure returned only contains an ExecutionTime and StatusCode keys. Is there any way I can access the error message which has been returned. e.g. To display to the user.

Thanks,

Tom

p.s. I would tag with "cfstoredproc" but I can't create new tags.

+1  A: 

Have you tried cfprocresult? Manual page says:

Associates a query object with a result set returned by a stored procedure.

Sergii
Hi, I tried the query in my original response with a cfprocresult. When dumping out this variable I get a variable undefined error.From reading the docs it looks like this is for returning querys whereas my stored procedure returns -1 and the error message.
Loftx
See, you have to do the SELECT to populate this variable, for example "SELECT 'this is error text' AS error". I'm not a SQL server guru, but I suppose it is possible to handle the error in the procedure and return the result using SELECT.
Sergii
+3  A: 

Not sure what DB you use but with Oracle I just use ColdFusion Exceptions to bubble up the Oracle exceptions. - #cfcatch.message# and #cfcatch.detail# are what you want to echo to the user.

<cftry>

<cfstoredproc  procedure = "my_Proc" dataSource = "#DB#" returnCode = "No">
   <cfprocparam type="in" cfsqltype="CF_SQL_VARCHAR" variable="myvar"   value="#someval#" null="No"> 
   <cfprocresult name="my_Response">
</cfstoredproc>

<cfcatch type="any">

   <cflog file="ProcError" text="Message = #cfcatch.message# Detail= #cfcatch.detail#">

</cfcatch>
</cftry>
kevink
I'm using SQL Server 2005. When I try wrapping a cftry/cfcatch block around my code it makes no difference - no coldfusion error is thrown so there's nothing to catch I guess.
Loftx
I confirm that using SQL Server 2005 and Adobe CF 8 a DO recieve raised error in cfcatch.Detail: [Macromedia][SQLServer JDBC Driver][SQLServer]Error 0 has occurred
Sergii
@Loftx - Seems odd but there is a setting in "Debug Output Settings" under "Custom Debugging Output" about showing exception info in debugging. I use the technique in servers with robust debugging off but that field may need to be checked. Is it on your box?
kevink