Showing posts with label Triggers. Show all posts
Showing posts with label Triggers. Show all posts

Friday 8 September 2017

SAP HANA : Triggers Before Update

Example Of Trigger Before Update:

Scenario:


            Whenever I update Unit price in my “PRODUCT TRIGGER” table. The old record should be inserted on the “PRODUCT PRICE HISTORY” table.

Here I have two table named as Product Trigger table and Product Price History table:


PRODUCT PRICE HISTORY Table 

PRODUCT TRIGGER Table


Now, I’m Trying to create a trigger with the concept whenever I updating in a “PRODUCT TRIGGER” table, the old record in the “PRODUCT TABLE” will be inserted in the “PRODUCT PRICE HISTORY” table based on “PRODUCT TRIGGER” table  “UNIT PRICE” with the time.


Code:

/*---------------------------------------------------------*/
CREATE trigger "KABIL_PRACTICE"."TRIGGER price_history_trigger"
BEFORE UPDATE OF unit_price
ON "KABIL_PRACTICE"."product_Trigger"  referencing old row as old
FOR EACH ROW
BEGIN
INSERT INTO "KABIL_PRACTICE"."product_price_history" VALUES
(
:old.product_id,
:old.product_name,
:old.supplier_name,
:old.unit_price,
Current_timestamp );
END;
          /*-----------------------------------------------------------*/


 Now update a record in PRODUCT TRIGGER table:

Code:

      update "KABIL_PRACTICE"."product_Trigger" set unit_price = 850 where product_id = 2;

Results:











Monday 31 July 2017

SAP HANA : Triggers After

v  This statement is used to triggers which is a set of statements that are executed when a given operation (INSERT/UPDATE/DELETE) takes place on a given subject table or subject view.
v  A trigger is also a stored procedure that automatically executes when an event happens on a given table or view.