The document describes the creation and functionality of database triggers for the RAAK_LEDGER_MASTER, RAAK_TRANS_MASTER, and RAAK_TRANS_DETAIL tables. These triggers ensure that certain fields are automatically converted to uppercase, generate unique record IDs, and populate ledger and voucher codes using sequences. Additionally, it explains the use of the NVL function for handling null values and the differences between :NEW and :OLD references in triggers.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
11 views4 pages
How To Manipulate Data Insert
The document describes the creation and functionality of database triggers for the RAAK_LEDGER_MASTER, RAAK_TRANS_MASTER, and RAAK_TRANS_DETAIL tables. These triggers ensure that certain fields are automatically converted to uppercase, generate unique record IDs, and populate ledger and voucher codes using sequences. Additionally, it explains the use of the NVL function for handling null values and the differences between :NEW and :OLD references in triggers.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4
Trigger is a database object.
This trigger can be initiated through APEX
Wizards and as well as through sql scripts. → Login Apex → SQL Workshop → Object Browser → Select Triggers in the left Window → CREATE → select the table name RAAK_LEDGER_MASTER → change the default constraint name as RAAK_LEDGER_MASTER_BI as constraint name → Firing Point as BEFORE → Insert against “OPTONS” → Click Tick FOR EACH ROW → Leave the when column option as blank → Write the body as :NEW."REPORT_TYPE" := UPPER(:NEW."REPORT_TYPE"); :NEW."LEDGER_NAME" := UPPER(:NEW."LEDGER_NAME"); IF :NEW."RECORD_ID" IS NULL THEN :NEW."RECORD_ID" := TO_NUMBER(SYS_GUID(),'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'); END IF; → Click NEXT → FINISH This trigger will generate system sequence number and will fill the record_id column. Any letters typed against report_type, ledger_name column will get converted as upper case. Record_id will have unique value even in transferring this table in to another database and it will not get 118 affected its uniqueness. Check constraint with respect to report_type will be checked after firing this trigger. On firing this trigger column will get uppercase letters. Further check constraint will check the validity of the data as 'BS' or 'PL' against report_type column. The above wizard option could have achieved through the following script triggers for raak_ledger_master. How to use before insert trigger create or replace trigger "RAAK_LEDGER_MASTER_BI" BEFORE insert on "RAAK_LEDGER_MASTER" for each row begin :NEW."REPORT_TYPE" := UPPER(:NEW."REPORT_TYPE"); :NEW."LEDGER_NAME" := UPPER(:NEW."LEDGER_NAME"); IF :NEW."RECORD_ID" IS NULL THEN :NEW."RECORD_ID" := TO_NUMBER(SYS_GUID(),'XXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXX'); END IF; end; We shall create the same system generated sequence script for raak_trans_master, raak_trans_detail also. While writing the trigger body, you shall find two new words like NULL, :NEW. Null means no data. It is not equivalent to zero, or spaces. Hence comparison cannot be made with null data. But wherever, if you want to 119 compare null column with some other data, you shall give assumption value through one function command. For example If you want to write as If record_id = 1 then In this case if record_id value is null then this comparison fails instead of that the following syntax may be used “if NVL(record_id, 0 ) = 1 then”. Here NVL function takes a role if the given parameter or column value is null or no data found then that may be replaced as zero. Hence NVL( ) is function which will accept two parameters such as column name, field name, variable name, separated by comma and then the required default value may be given as second parameters. :NEW. On seeing the word, we shall expect :OLD. Also. Generally :NEW. Followed by column name will indicate, new value of column name and :OLD.column name refers to old value available in the data storage. For example, there is Pay as column name in a table and on particular record it has got 1200. On editing user has replaced this value with 1500. :OLD.PAY → 1200 :NEW.PAY → 1500 another instance: at the time fresh record or on new insert, initial value may be null and then substituted value may be user‟s value :OLD.PAY → null :NEW.PAY → 1200 On this Before Insert Trigger with respect to Raak_Ledger_Master, we have one more requirement such as → Even if user enters ledger_name in lower case letters should be converted as upper case letters. Changes may be made on new record and 120 correcting the existing records also. Hence this trigger should get fired for converting the data into upper case during insert and update. → This conversion applicable for Report Type column too. Very slight modification need to be done on Before Insert Trigger: Before Modification of Trigger After Modification CREATE OR REPLACE TRIGGER "RAAK_ledger_MASTER_BI" BEFORE insert on "RAAK_ledger_MASTER" for each row begin if :NEW."RECORD_ID" IS NULL THEN :NEW."RECORD_ID" := TO_NUMBER(SYS_GUID(),'X XXXXXXXXXXXXXXXXXXXXXXX XXXXXXXX'); END IF; end; create or replace trigger "RAAK_LEDGER_MASTER_BI" BEFORE insert on "RAAK_LEDGER_MASTER" for each row begin :NEW."REPORT_TYPE" := UPPER(:NEW."REPORT_TYPE"); :NEW."LEDGER_NAME" := UPPER(:NEW."LEDGER_NAME" ); select :NEW."REPORT_TYPE"||'- '||LPAD(LEDGER_SEQ.NEXTVA L,4,'0')||'-00' into :NEW."LEDGER_CODE" from dual; IF :NEW."RECORD_ID" IS NULL THEN 121 :NEW."RECORD_ID" := TO_NUMBER(SYS_GUID(),'XX XXXXXXXXXXXXXXXXXXX XXXXXXXXXXX'); END IF; end; Please note the modifications: “insert on “ has been modified as “insert or update on” Two new lines has been added with respect to Ledger_name, Report_Type. Another new word / functions UPPER( ) has been used. This Upper() function will accept one parameter. For example Upper( 'raghu') will return value as RAGHU. Small / lower case letters has been embedded by single quotes in this function may be replaced by column name without single quotes. Assume that there is only one record wherein 'bank account' has been entered in ledger_name. If you give the command select ledger_name, upper(ledger_name) as caps from raak_ledger_master; will return Ledger_name caps 122 ---------------- -------- bank account BANK ACCOUNT Here, new word select is found. It is another command word like CREATE, ALTER SELECT means pick records. Select followed by column names and FROM is the must word to decide and should be followed with << table name >> . Another business rule needs to be incorporated in the same trigger. At the time of inserting new record, Ledger_code should be generated and inserted in the Ledger code. Ledger Code length is 10. Report Code Ledger-Sequence Group Code B S - 0 0 0 1 - 0 0 How to use before insert or update trigger CREATE OR REPLACE TRIGGER "RAAK_ledger_MASTER_BI" BEFORE INSERT OR UPDATE on "RAAK_LEDGER_MASTER" for each row begin if :NEW."RECORD_ID" IS NULL THEN :NEW."RECORD_ID" := TO_NUMBER(SYS_GUID(),'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'); select :NEW."REPORT_TYPE" ||'-'||LPAD(LEDGER_SEQ.NEXTVAL,4,'0')||'-00' into :NEW."LEDGER_CODE" from dual; END IF; :NEW.”LEDGER_NAME” := UPPER(:NEW.”LEDGER_NAME”); :NEW.”REPORT_TYPE”) := UPPER(:NEW.”REPORT_TYPE”); end; 123 In the above, you shall notice Ledger_code has been populated through the database sequence. Here .NEXTVAL increment the value after it has been used whereas if .currval followed with sequence name will produce the current sequence number and will not get incremented. THE following trigger can be used for RAAK_TRANS_MASTER CREATE OR REPLACE TRIGGER "RAAK_TRANS_MASTER_BI" BEFORE INSERT OR UPDATE on "RAAK_TRANS_MASTER" for each row begin :NEW.”VOUCHER_TYPE := UPPER(:NEW.”VOUCHER_TYPE”); :NEW.”VOUCHER_NARATION” := UPPER(:NEW.”VOUCEHR_NARATION”); if :NEW."RECORD_ID" IS NULL THEN :NEW."RECORD_ID" := RECORD_ID_SEQ.NEXTVAL ; select 'VOU-'||LPAD(VOUCHER_SEQ.NEXTVAL,6,'0') into :NEW."VOUCHER_NUMBER" from dual; END IF; end; The above trigger will help to convert upper case characters for of voucher_type, voucher_naration. Voucher_number will get populated using voucher_Seq created under database object. Dot and followed by nextval will increment the sequence number and padded with zeros with a prefix word as 'VOU-'. Here vou means voucher. 124 THE following trigger can be used for RAAK_TRANS_DETAIL CREATE OR REPLACE