Apache Hive : HPL/SQL - FETCH Statement

Last updated: August 12, 2026

Apache Hive : HPL/SQL - FETCH Statement

FETCH statement retrieve the next row from a cursor and assigns column values to local variable.

Syntax:

FETCH [FROM] cursor_name INTO var1 [, var2, ...];

Parameters:

ParameterTypeValueDescription
cursor_nameIdentifierThe name of the previously opened cursor
varNVariableA local variable

Examples:

DECLARE tabname VARCHAR DEFAULT 'db.orders';
DECLARE id INT;
DECLARE cur CURSOR FOR 'SELECT id FROM ' || tabname;
OPEN cur;
FETCH cur INTO id;
WHILE SQLCODE=0 THEN
  PRINT id;
  FETCH cur INTO id;
END WHILE;
CLOSE cur;

Compatibility: Oracle, IBM DB2, Teradata, SQL Server, MySQL, PostgreSQL and Netezza.

See also: