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
java sql-server-2008 stored-procedures callable-statement


source to share


No one has answered this question yet

Check out similar questions:

2108
How can I call one constructor from another in Java?
1818
How to get enum value from string value in Java?
1376
How to create Java string from file content?
348
Java 8 Lambda function that throws an exception?
five
Error while calling procedure from pyodbc
1
Active result for query does not contain laravel fields
1
java code makes the stored procedure return a result set at the DAO level
0
Displaying parameter number 2 is not an OUT parameter when calling a procedure using java
0
Mysql procedure containing both selection and update expression .... How to call such procedure from Java
-1
How to call MSSQL stored procedure using java



All Articles
Loading...
X
Show
Funny
Dev
Pics