title: Dynamic ORACLE SQL - How to convert a varchar2 variable to use as a column name in a select statement?
body: <p>I need to convert a varchar2(30) variable to use as a column name in a select statement or inside a sql function that is supposed to receive a column name.</p>
  
  <p>How can I do this?</p>
  
  <p>I'm trying to do some simple thing with dynamic sql like the following code, to solve this problem of varchar2 conversion but I cant get the expected result. It says "00900. 00000 - "invalid SQL statement" regarding the select statement in the query_string.</p>
  
  <p>Does someone know where is the bug here?</p>
  
  <pre><code> SET serveroutput ON;
   declare
   TYPE pointer IS REF CURSOR;
   yo pointer;
   var varchar2(20);
   query_string varchar2(200);
   num number;
  
   begin
   var := 'WORKERS'; --should be the column name!
   query_string := 'select'||var||'from MY_TABLE where MY_TABLE.ID = 2';
   OPEN yo FOR query_string;
   LOOP
   FETCH yo into num;
   EXIT WHEN yo%NOTFOUND;
   dbms_output.put_line(num);
   END LOOP;
   END ;
   /
  </code></pre>