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
Retrieves information about a condition handled in an exception handler, the active-transaction state, or the number of rows affected by the most recent DML statement.
The CONDITION form may only be used within a condition handler in a compound statement.
The TRANSACTION_ACTIVE form may be used as a standalone SQL statement or inside a compound statement.
The ROW_COUNT form may only be used within a compound statement, including the body of a SQL stored procedure.
Syntax
GET DIAGNOSTICS CONDITION 1
{ variable_name = condition_info_item } [, ...]
GET DIAGNOSTICS
{ variable_name = statement_info_item } [, ...]
condition_info_item
{ MESSAGE_TEXT |
RETURNED_SQLSTATE |
MESSAGE_ARGUMENTS |
CONDITION_IDENTIFIER |
LINE_NUMBER }
statement_info_item
{ TRANSACTION_ACTIVE |
ROW_COUNT }
Parameters
-
A local variable or session variable.
CONDITIONReturns the condition that triggered the condition handler. You must 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.
TRANSACTION_ACTIVEApplies to:
Databricks SQL
Databricks Runtime 18.2 and aboveReturns
1when the statement runs inside an atomic compound statement (BEGIN ATOMIC ... END); otherwise returns0.variable_namemust be anINT.Atomic compound statements provide the same multi-statement transactional semantics as interactive transactions; see
BEGIN ATOMICfor the surrounding scope.ROW_COUNTApplies to:
Databricks SQL
Databricks Runtime 18.3 and aboveReturns the number of rows affected by the most recently executed DML statement as a
BIGINT.variable_namemust be aBIGINT.Returns
NULLwhen:- No statement has executed yet in the enclosing compound body.
- The most recent statement is not a DML statement (for example, a
SELECT, DDL, orSET VAR). - The most recent DML statement does not report an affected-row count. Built-in Delta Lake writes —
INSERT,UPDATE,DELETE,MERGE INTO, andCOPY INTO— populateROW_COUNT. Writes to non-Delta tables and writes through Apache Spark data source V2 catalogs returnNULL. - The handler is entered. Each statement resets
ROW_COUNTbefore it runs, so an exception handler always observesNULL. - A
CALLstatement returns. Returning from a procedure resetsROW_COUNTin the caller, so the caller cannot observe DML executed inside the callee.
At most one variable per
GET DIAGNOSTICSstatement may be assigned fromROW_COUNT. Combine withTRANSACTION_ACTIVEby adding more assignments to the same statement.To capture the affected-row count, place
GET DIAGNOSTICS ... = ROW_COUNTimmediately after the DML statement. A nestedBEGIN ... ENDblock does not hide the outer block's DML — the value remains visible across nested compound bodies — but any intervening non-DML statement orCALLclears the slot.
Examples
-- Capture details of a handled condition in an exception handler.
> 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
-- Check whether the current statement runs inside an atomic transaction.
> DECLARE VARIABLE tx INT;
> GET DIAGNOSTICS tx = TRANSACTION_ACTIVE;
> SELECT tx;
0
> BEGIN ATOMIC
DECLARE tx INT;
GET DIAGNOSTICS tx = TRANSACTION_ACTIVE;
SELECT tx;
END;
1
-- Capture the number of rows affected by the most recent DML statement.
> CREATE OR REPLACE TABLE emp(name STRING, salary DECIMAL(10, 2));
> BEGIN
DECLARE rc BIGINT;
INSERT INTO emp VALUES ('Alice', 100.00), ('Bob', 200.00), ('Carol', 300.00);
GET DIAGNOSTICS rc = ROW_COUNT;
VALUES ('Inserted ' || rc || ' rows.');
DELETE FROM emp WHERE salary >= 200.00;
GET DIAGNOSTICS rc = ROW_COUNT;
VALUES ('Deleted ' || rc || ' rows.');
END;
Inserted 3 rows.
Deleted 2 rows.