Apache Hive : HPL/SQL - DECLARE HANDLER Statement

Last updated: August 12, 2026

Apache Hive : HPL/SQL - DECLARE HANDLER Statement

You can use DECLARE HANDLER statement to define one or more HPL/SQL statements to execute when a condition occurs.

Syntax:

DECLARE [CONTINUE | EXIT] HANDLER FOR 
  [SQLEXCEPTION | NOT FOUND | user_condition] code_block;

Description:

ParameterDescription
CONTINUEWhen the handler completes, control is returned to HPL/SQL statement following the statement that raised the condition
EXITAfter the handler completes, control is returned to the end of the block that declared the handler
code_blockHPL/SQL statement(s) to execute when the specified condition occurs

Examples:

DECLARE name VARCHAR(100);
DECLARE no_rows INT DEFAULT 0; 

DECLARE CONTINUE HANDLER FOR NOT FOUND
  SET no_rows = 1;    

OPEN cur FOR 'SELECT name FROM db.orders';

FETCH cur INTO name;
WHILE no_rows = 0 THEN  
  PRINT id;
  FETCH cur INTO name;
END WHILE;
CLOSE cur;

Compatibility: IBM DB2, Teradata and MySQL.

See also: