1

I'm trying to pick a random item from an array while performing an update in Oracle. What is the easiest way to achieve this?

I want something like this:

DECALRE ARRAY items = ('item1', 'item2', 'item3')

update table set field = items(TRUNCT(DBMS_RANDOM.value(1,3)))

that way a random value from the list is inserted in the field for each record that is being updated.

1
  • Why do you need an array to do this? Why not just update? Commented Jan 25, 2012 at 21:10

1 Answer 1

2

If you're using a fixed size array, you can use a VARRAY in PL/SQL:

DECLARE
    TYPE strArray IS VARRAY(3) of VARCHAR2(10);
    v_myarray strArray;
BEGIN
    v_myarray := strArray('item1', 'item2', 'item3');
    update table
    set field = v_myarray(DBMS_RANDOM.value(1,3));   
END;
/
Sign up to request clarification or add additional context in comments.

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.