SAP HANA Insert or Update Data Based on condition SQL Queries Example

 SAP  HANA Insert or Update Data Based on condition SQL Queries Example

Here is a way to decide whether to insert or update data on the condition of a variable content.



Example

  CREATE PROCEDURE PROC_INS_UPT (IN ID INTEGER, IN NAME VARCHAR(20), IN AGE INT)

  AS

  BEGIN

                DECLARE VI_ID_FOUND INT = 1;

    SELECT count(*) INTO VI_ID_FOUND FROM TABLE_TEST WHERE ID = :ID;

    IF :VI_ID_FOUND = 0

                THEN

          INSERT INTO TABLE_TEST

          VALUES (:ID, :NAME,:AGE);

      ELSE

          UPDATE TABLE_TEST SET AGE = :AGE WHERE ID = :ID;

      END IF;

  END;