title: Oracle 11 PL SQL Use Execute Immediate to assign value to variable
body: <p>I am new to PL SQL and still learning, but I need to solve a problem and I don't know enough about PL SQL to solve my problem quickly.</p>
  
  <p>I'm referencing two tables: users and attributes. I have a procedure that takes 3 arguments: <strong>attrib_id</strong>, <strong>uid</strong>,  <strong>attrib_value</strong>. </p>
  
  <p>I first queried the <strong>attributes</strong> table with the <strong>attrib_id</strong> to return an attribute name and assign it to a variable. My code works up to this point. </p>
  
  <p>Next I want to use the variable created from the previous select statement in another select statement to query the users table and return the current value associated with the attribute that the variable represents.</p>
  
  <p>CODE:</p>
  
  <pre><code>PROCEDURE value_update_proc_z(attrib_id INTEGER, uid IN VARCHAR2, attrib_value IN VARCHAR2)
  
  IS
      v_old_attrib_name attributes.attribute_name%TYPE;
      v_oldattrib_value varchar2(100);
      v_mymsg varchar2(2000);
  
  BEGIN
      EXECUTE IMMEDIATE 'SELECT attrib_name FROM attributes WHERE indx = ''' || attrib_id || '''' INTO v_old_attrib_name;
  
      EXECUTE IMMEDIATE 'SELECT' || v_old_attrib_name || 'FROM USERS WHERE USERID = ''' || uid || '''' INTO v_oldattrib_value;
  
      v_mymsg := v_old_attrib_name || ' ' || v_oldattrib_value;
  
  END value_update_proc_z;
  </code></pre>
  
  <p>The first query should return a value from the attributes table based on a number passed into the procedure. For example, if <strong>attrib_id</strong> = 1, the query would return first_name, if <strong>attrib_id</strong> = 2, then last_name returned, and if <strong>attrib_id</strong> = 3, the would be returned email. The returned value would be assigned to the variable <strong>v_old_attrib_name</strong>.</p>
  
  <p>Using the variable <strong>v_old_attrib_name</strong> in my select statement, I would expect that the second query would return a value, example; last_name would return williams, or email would return bob@somewhere.com. The result of this query would be assigned to the variable <strong>v_oldattrib_value</strong>.</p>
  
  <p>Currently, the first Execute Immediate works and when I display the message I can see the value of that variable, but when I add the second Execute Immediate, I get a message that the operation could not be completed. This isn't an error generated by the system, it is a message set up by a previous developer.</p>
  
  <p>I am open to suggestions for improvement.</p>
  
  <p>Thanks!</p>