Quantcast
Channel: SCN : Discussion List - SAP HANA Developer Center
Viewing all articles
Browse latest Browse all 6412

HANA SQL mystery: UPDATE statement INSERTS new row

$
0
0

I am new to HANA and found a strange phenomenon that I simplify in the following Code.

1.) HANA Code that inserts a new row when an UPDATE statement is executed.

2.) HANA Code that UPDATEs but does not INSERT.

3.) MySQL Code that is close to 1.) but gives the result in 2.) .


How can this be?

 

 

1.)

CREATE TABLE TEST1 ("ID" int);

INSERT INTO TEST1 VALUES (1);

INSERT INTO TEST1 VALUES (2);

 

ALTER TABLE TEST1 ADD  ("COLUMN_A" DOUBLE );

ALTER TABLE TEST1 ADD  ("COLUMN_B" DOUBLE );

 

CREATE TABLE TEST2 ("ID" INT,"COLUMN_A" INT);

INSERT INTO TEST2  VALUES (1, 99);

 

CREATE TABLE TEST3  ("ID" INT,"COLUMN_B" INT);

INSERT INTO TEST3  VALUES (2, -99);

 

SELECT * FROM TEST1;

SELECT * FROM TEST2;

SELECT * FROM TEST3;

 

UPDATE A  SET A.COLUMN_A  = B.COLUMN_A

FROM TEST1 AS A, TEST2 AS B

WHERE A.ID =  B.ID ;

 

UPDATE A  SET A.COLUMN_B  = B.COLUMN_B

FROM TEST1 AS A, TEST3 AS B

WHERE A.ID =  B.ID ;

 

UPDATE A  SET A.COLUMN_A  = B.COLUMN_A

FROM TEST1 AS A, TEST2 AS B

WHERE A.ID =  B.ID ;

 

SELECT * FROM TEST1;

 

The result of this Code is the following:

 

Unbenannt47.png

 

2.) Interestingly the following HANA Code does not produce this INSERT:

 

CREATE TABLE TEST1 ("ID" INT,"COLUMN_A" INT,"COLUMN_B" INT);

INSERT INTO TEST1 VALUES (1, 0, 0);

INSERT INTO TEST1 VALUES (2, 0, 0);

 

CREATE TABLE TEST2 ("ID" INT,"COLUMN_A" INT);

INSERT INTO TEST2  VALUES (1, 99);

 

CREATE TABLE TEST3  ("ID" INT,"COLUMN_B" INT);

INSERT INTO TEST3  VALUES (2, -99);

 

SELECT * FROM TEST1;

SELECT * FROM TEST2;

SELECT * FROM TEST3;

 

UPDATE A  SET A.COLUMN_A  = B.COLUMN_A

FROM TEST1 AS A, TEST2 AS B

WHERE A.ID =  B.ID ;

 

UPDATE A  SET A.COLUMN_B  = B.COLUMN_B

FROM TEST1 AS A, TEST3AS B

WHERE A.ID =  B.ID ;

 

SELECT * FROM TEST1;

 

Unbenannt48.png

3.) I am used to this from MySQL. This code works fine and produces the desired result altough the steps are the same as in the first HANA code above that generates an INSERT:

 

CREATE TABLE TEST1 (ID int);

    INSERT INTO TEST1 VALUES (1);

    INSERT INTO TEST1 VALUES (2);

 

    ALTER TABLE TEST1 ADD  (COLUMN_A DOUBLE );

    ALTER TABLE TEST1 ADD  (COLUMN_B DOUBLE );

 

    CREATE TABLE TEST2 (ID INT,COLUMN_A INT);

    INSERT INTO TEST2  VALUES (1, 99);

 

    CREATE TABLE TEST3  (ID INT,COLUMN_B INT);

    INSERT INTO TEST3  VALUES (2, -99);

 

    UPDATE TEST1 ,TEST2 SET TEST1.COLUMN_A  = TEST2.COLUMN_A WHERE TEST1.ID =  TEST2.ID ;

 

    UPDATE TEST1 ,TEST3 SET TEST1.COLUMN_B  = TEST3.COLUMN_B WHERE TEST1.ID =  TEST3.ID ;

   

    UPDATE TEST1 ,TEST2 SET TEST1.COLUMN_A  = TEST2.COLUMN_A WHERE TEST1.ID =  TEST2.ID ;

 

    SELECT * FROM TEST1;

 

Unbenannt48.png

 

 

Does anybody have an idea why the first HANA code above does not work? It can produce very harmful results.


Viewing all articles
Browse latest Browse all 6412

Trending Articles



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