0

i am using oracle sql developer and want to build a trigger to insert values into a table column.

My table is:

column1(num) | column2(num) | column3(var)
     1              5
     6              4
     7              3

I want to combine the first two columns, so in the end column3 should look like this:

column3(var)

       1_5
       6_4
       7_3

My Idea was:

create or replace TRIGGER   "Database"."TRIGGER"
BEFORE INSERT OR UPDATE ON   "Database"."TABLE"
FOR EACH ROW

BEGIN
    SELECT column1  ||  column2
    INTO :NEW.column3

    FROM TRIGGER;
    
END;

But column3 is still empty, can anybody tell me what i am doing wrong?

thanks in advance

1
  • 1
    As a side observation - do NOT enclose object names in double-quotes. This makes the names case sensitive, and you will always and forever have to enclose them in double-quotes, and use the correct case, when referencing them. And even that aside, I do hope your schema name 'Database', trigger name 'TRIGGER' and table name 'TABLE' are not the names you really use . . . Commented Apr 6, 2022 at 20:09

2 Answers 2

1

Rather than using a trigger, you can preferably add a virtual column after dropping the existing one such as

SQL> ALTER TABLE t DROP COLUMN col3;

SQL> ALTER TABLE t
ADD (
     col3 AS (col1||'_'||col2)
    );

which always will depend on those two columns, and any DML is not allowed, already not needed, it's good for displaying purposes with no interfering human factor.

Demo

Sign up to request clarification or add additional context in comments.

1 Comment

He will have to drop the existing col3 first; but this is definitely the best approach.
0
BEGIN
    :NEW.column3 :=column1  ||  column2;

END;

Calling a trigger name "Trigger" is a bad idea.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.