THROW or RAISERROR for specific non-user defined error codes - sql

I'm trying to test some code that traps a specific error code in SQL server. The error code is 7886.
When I try to THROW, I get:
Error number 7886 in the THROW statement is outside the valid range. Specify an error number in the valid range of 50000 to 2147483647.
When I try to RAISERROR, I get:
Error number 7886 is invalid. The number must be from 13000 through 2147483647 and it cannot be 50000.
Is there any way to raise an arbitrary error like this without actually setting up the situation to cause the error in the first place?

As said in the comments of the original question, this is impossible to do in SQL server.
:(

Related

How to get the actual wrong value from a broken query?

I have a very long query for insert, it gives me a "ORA-01722: invalid number" error, which is pretty simple thing usually. But this time i need to know what IN FACT value is wrong.
How do I do that?
I used construction
BEGIN
--longest spaghetti ever
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: '||sqlerrm);
END;
But it only gives me that damn error code and nothing more.
If you work on 10g or above, you can use DBMS_UTILITY.FORMAT_ERROR_STACK. It will return the full error message. With this, you can get the line number that you get error and you can zoom right in on the problem.
EXCEPTION
WHEN OTHERS THEN
my_putline (
DBMS_UTILITY.FORMAT_ERROR_STACK);
END;

SQL Server: how to test a trigger that doesn't throw errors but only prints statements?

My trigger checks if a condition is true, and if so throws a warning with a PRINT statement. This later gets caught by the JDBC, for use in the front-end.
My question: how can I make a test for the trigger in T-SQL? I have a testing USP that checks if ERROR_NUMBER equals the specified trigger error, but that only works if the trigger actually throws said error message, which this trigger isn't supposed to do.
Is there an alternative to using a PRINT perhaps, that I can catch in my testing USP, without interrupting the statement? (it is supposed to function anyway)
To make it more concrete: my database is about fishes and aquaria. When you put a carnivorous fish and a herbivorous fish in the same tank, it should give a warning (which my front-end will catch), but not restrict the action. How do I check if that warning was given using a testing USP?
I believe you are looking for RAISEERROR.
From the article:
RAISERROR can be used as an alternative to PRINT to return messages to calling applications. RAISERROR supports character substitution similar to the functionality of the printf function in the C standard library, while the Transact-SQL PRINT statement does not. The PRINT statement is not affected by TRY blocks, while a RAISERROR run with a severity of 11 to 19 in a TRY block transfers control to the associated CATCH block. Specify a severity of 10 or lower to use RAISERROR to return a message from a TRY block without invoking the CATCH block.
So you can use raiserror with a severity of 10 or lower if you don't want to go into the catch block or 11 to 19 if you want to take action (such as removing the inserted data) in the catch block.
Edit:
Since you can't use both msg_id and msg_str you would need to use sp_addmessage to add the error to the sys.messages catalog first then use msg_id instead of msg_string.
From the linked article:
msg_id Is a user-defined error message number stored in the sys.messages catalog view using sp_addmessage. Error numbers for user-defined error messages should be greater than 50000. When msg_id is not specified, RAISERROR raises an error message with an error number of 50000.

How to catch IDL "message" with /informational flag

I have a astronomy procedure that returns an error message, but it uses the /inf flag. With this flag, none of the normal error flags are set. So how would one catch such an error? For example, I call the procedure, it prints out the informational error message, but how can I check if it outputted such a message? I see it in the console, but how can the program check for this?
I don't believe that is possible. According to the docs, when the INFORMATIONAL keyword is set, !error_state is not changed.
Set this keyword to issue informational text instead of an error. In this case, !ERROR_STATE is not set. The !QUIET system variable controls the printing of informational messages.
I think you can do the following (so long as nothing happened in between):
MESSAGE,/REISSUE_LAST
assuming you have IDL version greater than 6.0. If not, then you can just go into the routine, modify it by defining a keyword that returns the message if it was printed to the screen, otherwise it returns nothing.

SQL CLR Aggregate function Error Handling

I have a user defined CLR aggregate function that can potentially throw an error. I would like to know how to handle an error if one occurs in my query.
The function is performing an IRR calculation similar to that which Excel does, ie. an iterative root-finding calculation. If no root is found, an error is thrown. This is the error I need to handle.
The query is part of a larger stored procedure and it looks something like:
select
MyID,
Excel_XIRR(col1)
from #t
group by MyID
and the error i get is something like this:
A .NET Framework error occurred during execution of user-defined routine or aggregate "Excel_Xirr":
System.ArgumentException: Not found an interval comprising the root after 60 tries, last tried was (-172638549748481000000000.000000, 280537643341281000000000.000000)
System.ArgumentException:
at System.Numeric.Common.rfindBounds#59(FastFunc`2 f, Double minBound, Double maxBound, Double precision, Double low, Double up, Int32 tries)
at System.Numeric.Common.findBounds(FastFunc`2 f, Double guess, Double minBound, Double maxBound, Double precision)
at System.Numeric.Common.findRoot(FastFunc`2 f, Double guess)
at Excel_Xirr.Terminate()
My problem is that not all the rows cause this error. There are some legitimate results from the query that I want to capture and use later in my stored procedure. Will this error stop me from getting the results of the query? If so, is there a way to figure out which rows throw the error (dynamically) and then rerun the query for the rest of the rows?
Not sure how well you have coded the XIRR function itself, looking at your function prototypes in Error messages it would seem you are using a Bi-section method of finding roots which is not most suitable to algorithms to use when it comes to finding rates. You will be locking yourself within a lower and upper bound no matter how large this bound is it is not going to help for all cases
As for solving your immediate problem with handling the error, you can change your .net code and replace the Throw...Exception statement with a return value of Math.Pow(-1, 0.5)
This will return a NAN to the calling program which you can then check with an IF statement to confirm whether your XIRR value is a number (when IRR is found) or a NAN value (when IRR is not found)

Arithmetic operation resulted in an overflow

When i send:
Result = CInt(NetApiBufferFree(pBuffer))
I receive (SOME TIMES) this error:
Arithmetic operation resulted in an overflow.
What exactly means that? and how i can resolve it?
It means that CInt argument is out of range of Integer, -0x80000000 to 0x7FFFFFFF
And it happens when NetApiBufferFree returns an error: error codes are bigger than 0x80000000.
There is no unsigned int32 type, so use CLng instead of CInt.
About source of error. You should find out code of error which you get: call MsgBox or log it to file, or use breakpoint. Next find its description. If it won't help you (for example error would be E_FAIL), add code to check that pBuffer value is valid - that it wasn't modified by something, and wasn't already freed. Add logging for NetApiBuffer* calls.
Best answer is replace 'double' in place of 'Int16/Int32/Int64'
some times file conversion takes huge numbers.. double never has maximum range.