Quantcast
Channel: SCN : Discussion List - SAP HANA and In-Memory Computing
Viewing all articles
Browse latest Browse all 5653

UPSERT without Primary Key

$
0
0

Is it possible to do an UPSERT with a subquery without referencing the Primary Key? From the documentation it seems like this would just lead to an insert:

 

"The UPSERT or REPLACE statement with a subquery works like the INSERT statement, except that if an old row in the table has the same value as a new row for a PRIMARY KEY, then the old row is changed by values of the returned record from a subquery. Unless the table has a PRIMARY KEY, it becomes equivalent to INSERT because there is no index to be used to determine whether or not a new row duplicates another."

 

Is there an equivalent for this?

 

create column table table1 (key1 varchar2(10) primary key, field1 varchar2(10), field2 varchar2(10));
create column table table2 (key1 varchar2(10) primary key, field1 varchar2(10), field2 varchar2(10));
insert into table1 values ('1', 'test1', 'test1');
insert into table2 values ('2', 'test1', 'test2');
merge into table1 t1 using (select key1, field1, field2 from table2) t2 on (t1.field1 = t2.field1)
when matched then
update set t1.field2 = t2.field2 
when not matched then insert (t1.key1, t1.field1, t1.field2) values (t2.key1, t2.field1, t2.field2);

 

So field2 for the single row in table1 would be updated based on the join to field1 rather than the primary key.

 

Thanks,

Jason


Viewing all articles
Browse latest Browse all 5653

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>