Trigger with random var inside?

  • Hello all!

    Example trigger:

    CREATE OR REPLACE TRIGGER dodaj_prispevek_trigger BEFORE INSERT ON PRISPEVKI

    FOR EACH ROW

    DECLARE

    st_prispevkov NUMBER;

    BEGIN

    SELECT COUNT(IDPRISPEVKA)

    INTO st_prispevkov

    FROM PRISPEVKI

    UPDATE ODBOR

    SET IDCLANA = !!! HERE I NEED A RADNOM INTEGER IN INTERVAL [1, count(IDCLANA)] !!!

    ....

    I googled but didn't find what i was lookin' for :\

    Can anybody help?

    Thanks!

  • Nevermind.. I solved the problem..

  • If we had found a solution for you, everyone would benefit because we would post the answer. To be courteous, you should too...

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Sorry, i thought this was to "easy"..

    Anyway here is the whole trigger:

    CREATE OR REPLACE TRIGGER dodaj_prispevek_trigger AFTER INSERT ON PRISPEVKI

    FOR EACH ROW

    DECLARE

    st_prispevka NUMBER;

    st_odobritve NUMBER;

    max_clan NUMBER;

    st_clana NUMBER;

    BEGIN

    -- dobimo id vstavljenega prispevka

    st_prispevka := :new.IDPRISPEVKA;

    -- izracunamo nov id odobritve

    SELECT MAX(IDODOBRITEV)

    INTO st_odobritve

    FROM ODOBRITVE;

    st_odobritve := st_odobritve + 1;

    SELECT MAX(IDCLANA)

    INTO max_clan

    FROM ODBORI;

    -- nakljucno izberemo clana odbora

    SELECT dbms_random.value(1, max_clan) num

    INTO st_clana

    FROM DUAL;

    -- vstavimo odobritev

    INSERT INTO ODOBRITVE (IDODOBRITEV, PRIS_IDPRISPEVKA,

    ODB_IDCLANA) VALUES (st_odobritve, st_prispevka, st_clana);

    END;

    /

    Bold is what i was lookin for 😉

  • Heh... thanks... good to know.

    And, my bad... didn't look at the first post... this is Oracle code...

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply