Calling procedure from Java and using RAISERROR to throw exceptions

I am calling MsSql procedures from JAVA to create invoices using the following code:

CallableStatement cstmt = conn.prepareCall(sql);
cstmt.registerOutParameter(1, java.sql.Types.VARCHAR);
cstmt.execute();
resultValue = cstmt.getString(1);
โ€ฆ

      

And in my procedure I use select @cErrorOut;

for debugging purposes.

But the catch is here, if I use select like in the example above, I don't get the SqlException from RAISERROR (@cErrorOut,16,1);

But if I remove the select everything looks fine. And if I call the procedure from ManagementStudio, I get the error as expected with or without selection. Why is this?

Here is a working and non-working example:

Example where we don't get sql exception due to selection (NOT OK):

-- create item  
EXEC dbo.pHE_MoveItemsCreAll @ahKey, @cIdent,@nQty, @cNote, @nhUserId, @nNo OUTPUT,@cErrorOut OUTPUT,@cStatus OUTPUT;  

select @cErrorOut;

-- check for error status
if (@cStatus <> 'T') begin
    EXEC dbo._CreateInvoiceRollback @ahKey;
    RAISERROR (@cErrorOut,16,1);
end;

      

Example where we get sql exception (OK):

-- create item  
EXEC dbo.pHE_MoveItemsCreAll @ahKey, @cIdent,@nQty, @cNote, @nhUserId, @nNo OUTPUT,@cErrorOut OUTPUT,@cStatus OUTPUT;  

-- check for error status
if (@cStatus <> 'T') begin
    EXEC dbo._CreateInvoiceRollback @ahKey;
    RAISERROR (@cErrorOut,16,1);
end;

      

Selection in variables works fine - exceptions are not affected:

select @anVat = anVAT from tHE_SetTax where acVATCode = @defaultVatCode;

      

Im using MsSql 2008 r2 server.

+3


source to share





All Articles