Use of Insert/Upsert in HANA SQL Script

light111Use of insert/upsert in SQL Script.


This post uses SCARR table to show the use of INSERT/UPSERT in hana sql script.

-> INSERT can be used to create  new record(s) with all columns or few columns including key columns.

-> UPSERT- Its INSERT or UPDATE . If the key values are already present then works as an update and if the key values not found then work as an INSERT. And can be used to update/insert desired columns or all columns.

The SCARR table has 5 fields.

1

Its a AMDP procedure , and the internal table contains 5 records and the INSERT with SELECT on the internal tables inserts 5 rows in the DB table.

This insert with select * , inserts all columns in the DB table.

2

The test report that calls amdp method.

3

Debug Details with internal table records- Do F8.

4

Now check the table and we have additional 5 records inserted.

5


For demo purpose, with help of debugger deleted the 5 records from DB table so that we can use the same data to show another use case.

6

Without using select all we can specify the field names also,  the select field names should match the DB table field names. This way of writing provides little flexibility which will see in next. 

7

Debug Details- F8.

8

Again it inserts the  5 records to the table.

9


Deleted records from the DB table-

10

Now this time need is to insert 3 records but not all columns, So in the INSERT statement the set of field names can be specified which to be inserted and similarly the follow on select should specify those fields that are provided in the INSERT field list.

11

Debug Details-

12

This time the insert works for 5 records but only for mentioned columns( 3 columns).

13


Deleted records from DB table-

15

Let’s see the use of UPSERT( its like MODIFY in ABAP open SQL).

The internal table LT_SCARR contains 5 records for which 3 records not present and other 2 records , the data is already present in the DB table.

16

Debug Details-

17

Now 3 rows inserted and for other 2 records are updated with 3 columns(carrname,currcode,url).

18


Deleted 3 inserted records from DB table:-

19

Now in UPSERT also, list of columns can be specified which to be updated/inserted.

20

Debug Details-

21

DB table Details- 3 records inserted with 3 columns and 2 records are updated with only one column(CARRNAME).

22


 

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 )

Google photo

You are commenting using your Google 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