简体   繁体   中英

Trigger after inserting on table

I am trying to create a trigger in Oracle that adds the username's full name after inserting into a table. I have already a trigger that adds the username in a ADDED_BY column after inserting into a table and my idea is basically to add the username's full name into a new column FULLNAME where FULLNAME = ADDED_BY.

This is what I have come with so far. This trigger adds the same FULL NAME and not the FULL NAME that corresponds to the username!

create or replace TRIGGER USERNAME_TO_FULLNAME
BEFORE INSERT OR UPDATE ON TABLE
FOR EACH ROW 

BEGIN 

  IF INSERTING THEN
    :new."ADDED_BY" := USER;
    :new."FULLNAME" := 'FULL NAME';
  ELSE
     :new."FULLNAME" := :old."FULLNAME";
  END IF;
END;

Edit: I have an Oracle Spatial database in a GIS environment. In this environment many people use the database to create objects (for example: drawing polygons that represent buildings and houses) and it becomes necessary in such an environment to keep track of who did what in the form of metadata. Suppose that there is a land surveyor whose name is George Clooney and he has the username GEOCLO in the database. When that user creates an object in the database, a trigger is fired to insert the value "GEOCLO" in a column called ADDED_BY (this trigger already exists in the database today) so that when people click on that object they know that it was created by the land surveyor Georgoe Clooney.

The problem is, after a couple of years George Clooney might change his role in the company and another person comes in and probably the manager of the GIS department changes their job so it becomes difficult to know who GEOCLO is/was! What I want to do is whenever George Clooney creates an object, a trigger is fired that inserts the value "George Clooney" to a column that is called FULL_NAME, just as the other trigger inserts the value "GEOCLO" to the ADDED_BY column.

What I was able to do so far, as mentioned in the original question, is to add the full name of one user to the FULL_NAME column after that user creates a new object and the question is how to add the names of the other users, in the same trigger, when they create new objects? Hope that the question is a little bit more clear now.

     IF INSERTING THEN
          :new."ADDED_BY" := USER;
          :new."FULLNAME" := :new."FULLNAME";
     ELSE
          :new."FULLNAME" := :old."FULLNAME";
     END IF;

:new."FULLNAME" := 'FULL NAME'; this mean you are assigning a string 'FULL NAME' to :new."FULLNAME" if you are inserting new values into "FULLNAME" column, use :new."FULLNAME" . I suppose mentioning this :new."FULLNAME" = :new."FULLNAME" is not required if new value is correct.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM