0

I would like to do the following in Oracle (PL/SQL) when defining a trigger:

    DECLARE
        pk_column_name VARCHAR(50) := 'id';
    BEGIN
    EXECUTE IMMEDIATE 'CREATE OR REPLACE TRIGGER table_a_trigger
        BEFORE INSERT ON table_a
        FOR EACH ROW
        BEGIN
            IF :new.pk_column_name IS NULL THEN       /* here */
                SELECT table_a_sequence.nextval
                INTO :new.pk_column_name              /* and here */
                FROM DUAL;
            END IF;
        END;';
    END;

I would like the variable pk_column_name (which contains the string 'id') to be "expanded" so I could write it like in above example:

    IF :new.pk_column_name  ....

instead of...

    IF :new.id  ....

I tried a lot of things like:

    :new."pk_column_name"
    :new.&pk_column_name
    :new.:pk_column_name

Could someone please help me?

Thanks, and best regards,

Udo

4
  • Why? You have to know the actual column name when you create the trigger in order to set the variable, so why not just use the actual name everywhere? You can't use a variable like that anyway... Commented Apr 21, 2017 at 18:15
  • No I don't know the actual column name, because this SQL is part of a code in a software, where the column name is not known before. Commented Apr 21, 2017 at 18:22
  • You can't create a trigger on a table if you don't know its structure at compile time. (Unless you don't refer to any of its columns, I suppose). Commented Apr 21, 2017 at 18:25
  • Why select from dual though? PL/SQL has this handy := assignment operator. Also in 12.1 you can set this as a column default. Commented Apr 22, 2017 at 22:48

1 Answer 1

0

I got it!

    ... IF :new.' || pk_column_name || ' IS NULL THEN ...

and

    ... INTO :new.' || pk_column_name || '
    FROM DUAL;
Sign up to request clarification or add additional context in comments.

7 Comments

Right, so you do know the column name at compile time. You didn't say you were creating it dynamically (which is an unusual thing to do).
Actually not. Not at compile time. The piece of software is used to dynamically rename a table and recreate triggers at runtime.
At the time the trigger is compiled (dynamically) you do know the PK column name, otherwise you couldn't concatenate it in like that.
Thanks for nothing. Sorry, but I think my example was quite clear to understand. I wanted to know how to expand the variable and use it in a trigger. You said it was not possible, but I found the solution myself.
Well, I was trying to help, but I'll free not to bother next time. Where in your question does it say that you're creating it dynamically? You showed the variable as local to the trigger, which it can't be, and isn't in your solution. I can only comment - and try to help - based on what you say in your question.
|

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.