1

I have a procedure like this:

PROCEDURE foo(
  p_field_name IN VARCHAR2,
  p_record IN table%rowtype )
IS
  v_value VARCHAR2(100);
BEGIN
  EXECUTE IMMEDIATE 'SELECT p_record.' || p_field_name || ' FROM dual' INTO v_value;
END

Oracle complains that p_record.my_field_name is an invalid identifier, which leads me to believe that the execute immediate is running in some other context.

Is it possible to do what I'm trying to do above?

1
  • 1
    what people are saying is, in fact, correct. You can't do this this way. If you want, post the 'big picture' question or feel free to send it to me. Commented Aug 20, 2009 at 14:09

3 Answers 3

1

What you do in EXECUTE IMMEDIATE should be doable in any context. You example does not fit this as it would not be doable outside of your procedure.

One possible workaround - pass ID instead of full row and select that single column from table. Although this could kill performance if used often.

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

1 Comment

Your comment re: the execute immediate context is as I suspected, though I started to wonder if something could be hacked together using sys_context()... And re: the workaround passing the ID, in one instance this function will already be called once per row in a loop through 700k rows of mapped text file. Another select is probably not cool though admittedly I have not profiled it. Thanks for the response.
1

What's wrong with a IF/ELSIF

IF    p_field_name = 'COL1' THEN v_value := p_record.col1
ELSIF p_field_name = 'COL2' THEN v_value := p_record.col2
...
END IF;

You could event generate that from a SELECT from ALL_TAB_COLUMNS. It may not be pretty but it would work.

1 Comment

It's just harder to maintain. Truth be told, I'm probably not going to leave this in the production code - I think I'll have to find a different way to do it. Thanks for your answer though.
1

I think what you are try to do is impossible. Execute immediate orders are dynamic SQL executions. The sql statment is not parsed when you create "foo" procedure, but at runtime. For oracle it is 'select p_record.' || p_field_name || ' from dual' is a varchar, and p_record too. So this explains why you have this error. The solution is to write something like if/elsif/... or case/when.

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.