Raise Exception issue in big query scripting - exception

I would like to print the custom message as an exception error with the RAISE keyword in big query script.
But, the below command lines is throwing an error at the raise command. But, if I remove the raise command it is working fine.
Could you please help me how I have to raise a custom error message??
Also, to know more about the RAISE [USING MESSAGE = message];.
BEGIN
SELECT 1/0; -- attempts to divide by zero
RAISE USING message = "divisible with zero is not allowed.";
EXCEPTION WHEN ERROR THEN
SELECT FORMAT("Hey, you. When you executed %s at %s, it caused an error: %s. Please don't do that.", ##error.statement_text, ##error.formatted_stack_trace, ##error.message);
END;

The RAISE statement must only be used within an EXCEPTION clause. The RAISE statement will re-raise the exception that was caught, and preserve the original stack trace.
Ref: https://cloud.google.com/bigquery/docs/reference/standard-sql/scripting#raise
So if you want to raise an exception with custom message, it should be:
BEGIN
SELECT 1/0; -- attempts to divide by zero
EXCEPTION WHEN ERROR THEN
RAISE USING message = FORMAT("Hey, you. When you executed %s at %s, it caused an error: %s. Please don't do that.", ##error.statement_text, ##error.formatted_stack_trace, ##error.message);
END;

Related

What happens when a exception occurs in exception block

suppose in my procedure, error occurred and it went to exception block.
while entering the logs for error occurred another error takes placein exception block itself.
What happens then ...
The easiest way to find out is surely to try it?
declare
dummy integer;
begin
select 99 into dummy from all_objects; -- Will raise TOO_MANY_ROWS
exception
when too_many_rows then
select 77 into dummy from dual where 2=3; -- Will raise NO_DATA_FOUND
end;
/
When you run that you will get:
ORA-01403: no data found
ORA-06512: at line 8

SQLSTATE that returns an error but completes insert

I am trying to write an after insert trigger in mariadb that displays an error but doesn't undo the insert.
I read here mySQL documentation that
"If pval is anything else, p() first signals a warning and sets the message text and error number condition information items. This warning does not terminate the procedure, so execution continues and p() then signals an error. The error does terminate the procedure. The message text and error number set by the warning are replaced by the values set by the error, which are returned with the error information."
This is exactly what I'm trying to do. The trigger is just suppose to display essentially a warning, something along the lines of "insert successful... but hours > allowed_hours" but for my purpose it has to be returned as an error. Does anyone know what the documentation means by "If pval is anything else..."?
Thanks!

No data found not executing in trigger

The below portion is the relevant part of my trigger
BEGIN SELECT rec_date
INTO lv_rec_date
FROM receipts_table
WHERE receipts_number = new.receipts_number
EXCEPTION WHEN NO_DATA_FOUND THEN
lv_partial := FALSE;
END;
The problem is that when it executes this portion, instead of capturing it nicely, I get the following error
ERROR at line 1:
ORA-01403: no data found
ORA-06512: at "APP.BEF_ROW_TRIGGER", line 66
ORA-04088: error during execution of trigger 'APP.BEF_ROW_TRIGGER'
This worked fine in Oracle 10 and below. I checked and line 66 in the USER_SOURCE for this trigger is exactly the EXCEPTION WHEN NO_DATA_FOUND THEN line. It seems to be hitting the NO_DATA_FOUND error, but the error catching doesn't seem to be working.
Are there any changes in Oracle 11g that I should be aware of?
Never mind, false alarm ^_^. The error was on line 66 of a procedure being called AFTER this section, but the error message was showing it was from the trigger. Ouch.....
Sorry for the trouble

Is there a way to get the line of MySQL code that caused a SQLEXCEPTION in my stored procedure?

I use this code in my stored procedure to get info on a SQLEXCEPTION:
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
GET DIAGNOSTICS condition 1
#SQLState = RETURNED_SQLSTATE, #SQLMessage = MESSAGE_TEXT;
SELECT CONCAT('Database error occurred, state - ',#SQLState, '; error msg - ', #SQLMessage) INTO #errorString;
CALL Collectors_Errors
(#errorString,
'Collectors_UpdateGame',
barcodeApp,
usernameApp);
END;
The problem with this code is even though I know what the error is, it doesn't tell me at which line it occurred. And some of my sprocs are very long.
Is there a way to determine which line number, or what the line was, that caused the exception?
You can try the rdebug tool for debugging your stored procedures. I have not used it, but I find the rest of the tools in common_schema to be super helpful.
There may be an easier quick fix for finding exactly where this one error comes from, but in you are working with long stored procedures you may find rdebug to be helpful beyond this one error.

How to log errors from MySQL-ODBC-driver in Microsoft SQL Server

I want to log errors from my odbc-connection. For example it should be possible to see a simple syntax error. This works great:
BEGIN TRY
Declare #t as money
set #t = 1/0
END TRY
BEGIN CATCH
Print ERROR_MESSAGE();
END CATCH
The query returns the error message. Works great.
This also works, because there seems to be a red message?
BEGIN TRY
EXEC('Selllect 1/0 as Column1 ') AT VERBINDUNGSSERVERNAME
END TRY
BEGIN CATCH
Print ERROR_MESSAGE();
END CATCH
Screenshot:
But unfortunately not every ODBC error was logged. But I want to log all of these odbc errors. The next example is not logged by the try-catch block:
I'm using a lot of query stacks and it would be helpful to get a valid error log :-) Thank you!