cancel
Showing results for 
Search instead for 
Did you mean: 

UPSERT in SAP HANA 2.0 SPS5 without updating a particular column.

former_member565459
Participant
0 Kudos
1,036

Hello,

I've a requirement where I want to insert/update into a table based on a primary key.

I've one column named RECORD_DATE whose default value I've set to current_timestamp.

Now when I'm getting a new record on the same primary key, I don't want to update the record_date column with current_timestamp.

In current scenario, my record_date column also gets updated to current_timestamp.

Is there a way on how can I achieve this?

CREATE COLUMN TABLE PH_UPSERT_CHECK(
ID INT,
NAME NVARCHAR(10),
RECORD_DATE SECONDDATE DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT CPK_UP PRIMARY KEY(ID));

UPSERT PH_UPSERT_CHECK(ID,NAME)VALUES(1,'PH') WITH PRIMARY KEY;

1. When I run this record on an empty table, record_date should get updated(as it is insert)

2. When I re-run this record on the table for the next time with some change in column NAME (update)

the record_date column shouldn't be updated.

florian.pfeffer can you help me with the same?

TIA.

Regards,

Prathamesh H.

View Entire Topic
pfefferf
Active Contributor

prathamesh_h As I don't know the internal implementation of the command I cannot say why exactly this happens, but I can say under which condition it happens. And it seems that the behaviour is related to the "WITH PRIMARY KEY" clause.

If you do the statement with "WITH PRIMARY KEY" clause the RECORD_DATE column is updated with a "new" default value:

UPSERT PH_UPSERT_CHECK(ID,NAME) VALUES (1,'PH') WITH PRIMARY KEY;<br>

If you don't use the "WITH PRIMARY KEY" clause and just use a "WHERE" clause for the primary key, the RECORD_DATE column is not updated.

UPSERT PH_UPSERT_CHECK(ID,NAME) VALUES(1,'PH') WHERE ID = 1;<br>

An insert is done too if no record with the ID specified in the WHERE clause is existing.

An interesting behavior, for which I would expect an explanation by SAP as it is not really clear to me why the behavior is like it is.

PS: I tested this on SAP HANA Cloud, not on an On-Premise 2.0 SPS05 installation (maybe you can verify that on your system too).

former_member565459
Participant
0 Kudos

Thanks, Florian.

Exactly, even I tried using the same way and the record date wasn't getting updated.

But, not very clear as to why there is such weird behaviour.