Solved

A UNIQUE TRIGGER ON TWO COLUMNS - ORACLE PL/SQL PROGRAMMING

Posted on 2008-10-13
3
961 Views
Last Modified: 2013-12-18
Good evening!
In a table called NEREPSTA_CONFIG, I have a column called REPSTA_SFTP_PASSWORD (type VARCHAR2), on which I've built the following trigger:
CREATE OR REPLACE TRIGGER MASTER.REPSTA_SOURCE
BEFORE INSERT OR UPDATE
OF REPSTA_SFTP_PASSWORD
ON MASTER.NEREPSTA_CONFIG
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
BEGIN
   :NEW.repsta_sftp_password := cripta (:NEW.repsta_sftp_password);
END;
/

So, when a new record is inserted, on commit the NEREPSTA_SFTP_PASSWORD column is encrypted with che "CRIPTA" function.  Moreover, when the field NEREPSTA_SFTP_PASSWORD is updated, on commit it's encrypted with the same "CRIPTA" function.
Now please suppose to ALTER the NEREPSTA_CONFIG table, adding a new column called NGNEER_SFTP_PASSWORD: I would like to modify the trigger in order to use the CRIPTA stored function ONLY on the field updated or inserted, not on both fields inconditionally: only the modified field has to be cripted with the CRIPTA function.  Of course, if you INSERT a NEW record, both the fields NEREPSTA_SFTP_PASSWORD and NGNEER_SFTP_PASSWORD has to be crypted.

Any clues??
0
Comment
Question by:CRISTIANO_CORRADI
  • 2
3 Comments
 
LVL 29

Accepted Solution

by:
MikeOM_DBA earned 500 total points
Comment Utility

Try this:

CREATE OR REPLACE TRIGGER MASTER.REPSTA_SOURCE

BEFORE INSERT OR UPDATE

OF REPSTA_SFTP_PASSWORD, NGNEER_SFTP_PASSWORD

ON MASTER.NEREPSTA_CONFIG

REFERENCING NEW AS NEW OLD AS OLD

FOR EACH ROW

BEGIN

  IF INSERTING THEN

   :NEW.repsta_sftp_password := cripta (:NEW.repsta_sftp_password);

   :NEW.ngneer_sftp_password := cripta (:NEW.ngneer_sftp_password);

  ELSE

    IF UPDATING('REPSTA_SFTP_PASSWORD') THEN

     :NEW.repsta_sftp_password := cripta (:NEW.repsta_sftp_password);

    ELSE

      IF UPDATING('NGNEER_SFTP_PASSWORD ') THEN

       :NEW.ngneer_sftp_password := cripta (:NEW.ngneer_sftp_password);

      END IF;

    END IF;

  END IF;

END;

/

Open in new window

0
 

Author Comment

by:CRISTIANO_CORRADI
Comment Utility
Thanks for your help: I've modified your source code as follows.

500 points for you ;-)
CREATE OR REPLACE TRIGGER MASTER.repsta_source

   BEFORE INSERT OR UPDATE OF repsta_sftp_password, ngneer_sftp_password

   ON MASTER.nerepsta_config

   REFERENCING NEW AS NEW OLD AS OLD

   FOR EACH ROW

BEGIN

   IF INSERTING

   THEN

      BEGIN

         :NEW.repsta_sftp_password := cripta (:NEW.repsta_sftp_password);

         :NEW.ngneer_sftp_password := cripta (:NEW.ngneer_sftp_password);

      END;

   END IF;
 

   IF UPDATING ('REPSTA_SFTP_PASSWORD')

   THEN

      :NEW.repsta_sftp_password := cripta (:NEW.repsta_sftp_password);

   END IF;
 

   IF UPDATING ('NGNEER_SFTP_PASSWORD')

   THEN

      :NEW.ngneer_sftp_password := cripta (:NEW.ngneer_sftp_password);

   END IF;

END;

/

Open in new window

0
 

Author Closing Comment

by:CRISTIANO_CORRADI
Comment Utility
Thank you very much for your fast, kind help ;-)
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now