OceanBase logo

OceanBase

A unified distributed database ready for your transactional, analytical, and AI workloads.

Product Overview
DEPLOY YOUR WAY

OceanBase Cloud

The best way to deploy and scale OceanBase

OceanBase Enterprise

Run and manage OceanBase on your infra

TRY OPEN SOURCE

OceanBase Community Edition

The free, open-source distributed database

OceanBase seekdb

Open source AI native search database

Customer Stories

Real-world success stories from enterprises across diverse industries.

View All
BY USE CASES

Mission-Critical Transactions

Global & Multicloud Application

Elastic Scaling for Peak Traffic

Real-time Analytics

Active Geo-redundancy

Database Consolidation

Resources

Comprehensive knowledge hub for OceanBase.

Blog

Live Demos

Training & Certification

Documentation

Official technical guides, tutorials, API references, and manuals for all OceanBase products.

View All
PRODUCTS

OceanBase Cloud

OceanBase Database

Tools

Connectors and Middleware

QUICK START

OceanBase Cloud

OceanBase Database

BEST PRACTICES

Practical guides for utilizing OceanBase more effectively and conveniently

Company

Learn more about OceanBase – our company, partnerships, and trust and security initiatives.

About OceanBase

Partner

Trust Center

Contact Us

International - English
中国站 - 简体中文
日本 - 日本語
Sign In
Start on Cloud

OceanBase

A unified distributed database ready for your transactional, analytical, and AI workloads.

Product Overview
DEPLOY YOUR WAY

OceanBase Cloud

The best way to deploy and scale OceanBase

OceanBase Enterprise

Run and manage OceanBase on your infra

TRY OPEN SOURCE

OceanBase Community Edition

The free, open-source distributed database

OceanBase seekdb

Open source AI native search database

Customer Stories

Real-world success stories from enterprises across diverse industries.

View All
BY USE CASES

Mission-Critical Transactions

Global & Multicloud Application

Elastic Scaling for Peak Traffic

Real-time Analytics

Active Geo-redundancy

Database Consolidation

Comprehensive knowledge hub for OceanBase.

Blog

Live Demos

Training & Certification

Documentation

Official technical guides, tutorials, API references, and manuals for all OceanBase products.

View All
PRODUCTS
OceanBase CloudOceanBase Database
ToolsConnectors and Middleware
QUICK START
OceanBase CloudOceanBase Database
BEST PRACTICES

Practical guides for utilizing OceanBase more effectively and conveniently

Learn more about OceanBase – our company, partnerships, and trust and security initiatives.

About OceanBase

Partner

Trust Center

Contact Us

Start on Cloud
编组
All Products
    • Databases
    • iconOceanBase Database
    • iconOceanBase Cloud
    • iconOceanBase Tugraph
    • iconInteractive Tutorials
    • iconOceanBase Best Practices
    • Tools
    • iconOceanBase Cloud Platform
    • iconOceanBase Migration Service
    • iconOceanBase Developer Center
    • iconOceanBase Migration Assessment
    • iconOceanBase Admin Tool
    • iconOceanBase Loader and Dumper
    • iconOceanBase Deployer
    • iconKubernetes operator for OceanBase
    • iconOceanBase Diagnostic Tool
    • iconOceanBase Binlog Service
    • Connectors and Middleware
    • iconOceanBase Database Proxy
    • iconEmbedded SQL in C for OceanBase
    • iconOceanBase Call Interface
    • iconOceanBase Connector/C
    • iconOceanBase Connector/J
    • iconOceanBase Connector/ODBC
    • iconOceanBase Connector/NET
icon

Embedded SQL in C for OceanBase

V1.1.7Enterprise Edition

  • What's New
  • What is ECOB?
  • ECOB compatibility with Pro*C
  • Install ECOB
  • Data types and variables
    • Overview
    • Supported data types
    • Data type conversion
    • Indicator variable
    • VARCHAR variable
    • Structure
    • String pointer
  • Embedded SQL statements
    • Overview
    • Variable declaration
    • Connect to OceanBase databases
    • Basic SQL statements
    • Prepared statements
    • Stored procedures
    • Cursor
    • Simple dynamic SQL statement
    • ANSI dynamic SQL statements
    • Error handling
      • SQLCA structure
      • WHENEVER statement
    • LOB-related SQL statements
  • Use ECOB
    • Environment preparation
    • Use ECOB
    • Compile the Tuxedo service
  • Command Line Options

Download PDF

What's New What is ECOB? ECOB compatibility with Pro*C Install ECOB Overview Supported data types Data type conversion Indicator variable VARCHAR variable Structure String pointer Overview Variable declaration Connect to OceanBase databases Basic SQL statements Prepared statements Stored procedures Cursor Simple dynamic SQL statement ANSI dynamic SQL statements SQLCA structure WHENEVER statement LOB-related SQL statements Environment preparation Use ECOB Compile the Tuxedo service Command Line Options
OceanBase logo

The Unified Distributed Database for the AI Era.

Follow Us
Products
OceanBase CloudOceanBase EnterpriseOceanBase Community EditionOceanBase seekdb
Resources
DocsBlogLive DemosTraining & Certification
Company
About OceanBaseTrust CenterLegalPartnerContact Us
Follow Us

© OceanBase 2026. All rights reserved

Cloud Service AgreementPrivacy PolicySecurity
Contact Us
Document Feedback
  1. Documentation Center
  2. Embedded SQL in C for OceanBase
  3. V1.1.7
iconEmbedded SQL in C for OceanBase
V 1.1.7Enterprise Edition

Cursor

Last Updated:2023-06-29 11:12:55  Updated
share
What is on this page
DECLARE CURSOR
OPEN
FETCH
CLOSE
ALLOCATE statement

folded

share

OceanBase Embedded SQL in C (ECOB) supports the following cursor-related statements: DECLARE CURSOR, OPEN, FETCH, CLOSE, and ALLOCATE.

DECLARE CURSOR

You can execute the DECLARE CURSOR statement to declare a cursor variable. Cursor variables support only query statements.

Syntax:

EXEC SQL DECLARE <cur_name> [SCROLL] CURSOR [WITH HOLD] FOR (select_stat | :host_variable | prepare_id)

Screenshot 2020-10-14 23.09.33.png

The SCROLL parameter specifies the cursor as a scroll cursor. If the MODE parameter is set to ANSI (By default, the MODE parameter is set to Oracle), the cursor will be closed when the COMMIT statement is executed. You can use the WITH HOLD keyword in the command to prevent the cursor from being closed when the COMMIT statement is executed.

Sample statements:

char * sql_stat = "select c1,c2 from t1";
EXEC SQL PREPARE stat from :sql_stat;
EXEC SQL DECLARE cur CURSOR for stat;

OPEN

You can execute the OPEN statement to open a cursor variable.

Syntax:

EXEC SQL OPEN <cur_name> [ USING :host_variable ]

Screenshot 2020-10-14 23.11.04.png

Sample statements:

int c2val = 0;
char * sql_stat = "select c1,c2 from t1";
EXEC SQL PREPARE stat from :sql_stat;
EXEC SQL DECLARE cur CURSOR for stat;
EXEC SQL OPEN cur;
EXEC SQL DECLARE cur1 SCROLL CURSOR for select c1 from t1 where c2 > :c2;
EXEC SQL OPEN cur1 USING :c2val;

FETCH

You can execute the FETCH statement to get the result set stored by the cursor variable. For a scroll cursor, you can specify the position to fetch the scroll cursor.

Syntax:

EXEC SQL FETCH [ ABSOLUTE | RELATIVE (:host_variable | n )] [FIRST | PRIOR | NEXT|LAST| CURRENT] <cur_name>  INTO :host_variables [ [INDICATOR] :indicator_variable]

E91212E2-5469-4265-89F3-7B73B1DC27D5

The ABSOLUTE parameter specifies the cursor position and can be followed by a host variable or numeric constant. You can use the NEXT keyword to push a cursor backward. At present, you can push a cursor backward, but not forward.

Sample statements:

int c2val = 0;
int c1val = 0;
int pos = 2;
short ind;
EXEC SQL DECLARE cur1 SCROLL CURSOR for select c1 from t1 where c2 > :c2;
EXEC SQL OPEN cur1 USING :c2val;
EXEC SQL FETCH FIRST cur1 INTO :c1val:ind;
EXEC SQL FETCH PRIOR cur1 INTO :c1val:ind;
EXEC SQL FETCH LAST cur1 INTO :c1val:ind;
EXEC SQL FETCH NEXT cur1 INTO :c1val:ind;
EXEC SQL FETCH ABSOLUTE :pos INTO :c1val:ind;
EXEC SQL FETCH ABSOLUTE 3 INTO :c1val;
EXEC SQL FETCH RELATIVE :pos INTO :c1val:ind;
EXEC SQL FETCH RELATIVE 4 INTO :c1val;
EXEC SQL CLOSE cur1;

CLOSE

The CLOSE statement is used to close a cursor variable.

Syntax:

EXEC SQL CLOSE <cur_name>

Screenshot 2020-10-14 22.03.13.png

Sample statements:

int c1val;
EXEC SQL CREATE TABLE t1 (c1 int);
EXEC SQL INSERT INTO t1 VALUES(1);
EXEC SQL COMMIT WORK;
EXEC SQL DECLARE cur CURSOR FOR select c1 from t1;
EXEC SQL OPEN cur;
EXEC SQL WHENEVER NOT FOUND DO BREAK;
for(;;){
EXEC SQL FETCH cur into :c1val;
}
EXEC SQL CLOSE cur;

ALLOCATE statement

The ALLOCATE statement is used to allocate a cursor variable.

Syntax:

EXEC SQL ALLOCATE :sql_cursor;

Logic diagram

When a cursor variable is allocated, the server does not need to be called during precompilation or running. If the ALLOCATE statement contains errors, such as an undeclared host variable, ECOB will return a precompilation error. The stack memory is used when a cursor variable is allocated. Therefore, you can release a cursor variable during a loop of the program.

Sample statement:

SQL_CURSOR sql_cursor;
char col2[100]={0};
EXEC SQL ALLOCATE :sql_cursor;
EXEC SQL OPEN :sql_cursor FOR SELECT col2 FROM test;
EXEC SQL FETCH :sql_cursor INTO :col2;
EXEC SQL CLOSE :sql_cursor;
EXEC SQL FREE :sql_cursor;

Previous topic

Stored procedures
Last

Next topic

Simple dynamic SQL statement
Next
What is on this page
DECLARE CURSOR
OPEN
FETCH
CLOSE
ALLOCATE statement