title: Dynamically assigning variables oracle sql
body: <p>I have a table attribute_config with below columns:</p>
  
  <p>table_name column_name key </p>
  
  <p>Let us say is has below 2 rows</p>
  
  <p>account    accountphone accountnum</p>
  
  <p>customer   customernumber customerid</p>
  
  <p>Key can be only accountnum or customerid.</p>
  
  <p>I have to write code which will accept (i_accountnum,i_customerid) and;</p>
  
  <p>fetch the respective values from columns mentioned in column_name in tables mentioned in table_name using the key in where condition.</p>
  
  <p>For ex: select accountphone from account where accountnum = i_accountnum
      select customernumber from customer where customerid = i_customerid</p>
  
  <p>the complete query should be formed dynamically, whether to pass i_accountnum or i_customerid in the query also needs to be decided dynamically. if key - accountnum, i_accountnum will be passed to where condition.</p>
  
  <p>I have been trying on these lines so far, this is not working, i know it is wrong.</p>
  
  <pre><code>declare
  v_accountnum varchar2(20);
  v_customerid varchar2(20);
  v_attribute_value varchar2(20);
  v_stmt varchar2(255);
  begin
  Account_Num := 'TestCustomer';  -- input to the function
  v_customer_ref := 'TestAccount'; -- input to the function
  for i in (Select * from attribute_config) loop
  v_stmt := 'select ' || i.column_name || ' from ' ||  i.table_name ||' where ' || i.key|| ' = v_' || i.key;
  execute immediate v_Stmt into v_attribute_value;
  end loop;
  end;
  </code></pre>