Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Applies to:
Databricks SQL
Databricks Runtime 16.3 and above
Important
This feature is in Public Preview.
Retrieve information about a condition handled in an exception handler.
This statement may only be used within a condition handler in a compound statement.
Syntax
GET DIAGNOSTICS CONDITION 1
{ variable_name = condition_info_item } [, ...]
condition_info_item
{ MESSAGE_TEXT |
RETURNED_SQLSTATE |
MESSAGE_ARGUMENTS |
CONDITION_IDENTIFIER |
LINE_NUMBER }
Parameters
-
A local variable or session variable.
CONDITION 1Returns the condition that triggered the condition handler. You must call issue
GET DIAGNOSTICS CONDITION 1as the first statement in the handler.MESSAGE_TEXTReturns the message text associated with the condition as a
STRING.variable_namemust be aSTRING.RETURNED_SQLSTATEReturns the
SQLSTATEassociated with the condition being handled as aSTRING.variable_namemust be aSTRING.MESSAGE_ARGUMENTSReturns a
MAP<STRING, STRING>mapping provided as arguments to the parameters of Databricks conditions. For declared conditions, the only map key isMESSAGE_TEXT.variable_namemust be aMAP<STRING, STRING>CONDITION_IDENTIFIERReturns the condition name that caused the exception.
variable_namemust be aSTRING.LINE_NUMBERReturns the line number of the statement raising the condition.
NULLif not available.
Examples
-- Retrieve the number of rows inserted by an INSERT statement
> CREATE OR REPLACE TABLE emp(name STRING, salary DECIMAL(10, 2));
> BEGIN
DECLARE EXIT HANDLER FOR DIVIDE_BY_ZERO
BEGIN
DECLARE cond STRING;
DECLARE message STRING;
DECLARE state STRING;
DECLARE args MAP<STRING, STRING>;
DECLARE line BIGINT;
DECLARE argstr STRING;
DECLARE log STRING;
GET DIAGNOSTICS CONDITION 1
cond = CONDITION_IDENTIFIER,
message = MESSAGE_TEXT,
state = RETURNED_SQLSTATE,
args = MESSAGE_ARGUMENTS,
line = LINE_NUMBER;
SET argstr = array_join(transform(map_entries(args), t -> concat_ws(' ', 'Param:', t.key, 'Val:', t.value)), ' ');
SET log = 'Condition: ' || cond ||
' Message: ' || message ||
' SQLSTATE: ' || state ||
' Args: ' || argstr ||
' Line: ' || line;
VALUES (log);
END;
SELECT 10/0;
END;
Condition: DIVIDE_BY_ZERO Message: Division by zero. Use try_divide to tolerate divisor being 0 and return NULL instead. If necessary, set <config> to “false” to bypass this error. SQLATTE: 22012 Args: Parm: config Val: ANSI_MODE Line: 28