HANA SQL- Cursor

light111CURSOR in HANA SQL


A cursor is bound to a query  which optionally can be parameterized and is used to fetch single row at a time from the query result set. But it is recommended to avoid use of cursor if same thing can be achieved by SQL as the cursor can not be optimized like SQL.

A cursor has 4 parts- Declaration, OPEN, FETCH and CLOSE.  There are few cursor attributes that tells whether it found the result or not(NOTFOUND) or the cursor is open or close( ISCLOSED).  Let’s start to see the details-

Below we have a AMDP Procedure with a CURSOR (parameterized) and bound to the query. Later point of time we can use OPEN & FETCH to pass the input and get the result and finally CLOSE the cursor.

1

The test program that calls the amdp method.

2

Debug details- 

3

The result set gives the value-

4

The result set gives the value-

5

Another result-

6

Now let’s use some of the cursor attributes –

ISCLOSED – returns YES or NO depending on the cursor is close or open.

NOTFOUND- returns YES or NO depending on the row found or not from the query result set.

7

Debug details-

8

It is quite possible to LOOP over the CURSOR to get all the rows one by one from the query result set. 

9

Test Program-

10

In the underlying DB we have 5 records-

11

If we loop then we can see all rows(few rows details shown)-

14

 

12

13


 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s