Home » SQL & PL/SQL » SQL & PL/SQL » Dynamic SQL - How to convert a varchar2 variable to use as a column name in a select statement (Oracle SQL)
Dynamic SQL - How to convert a varchar2 variable to use as a column name in a select statement [message #634540] Wed, 11 March 2015 04:55 Go to next message
Adam Brave
Messages: 12
Registered: March 2015
Location: Lisboa
Junior Member
I need to convert a varchar2(30) variable to use as a column name in a select statement? How can I do this?

Thanks in advance.

Re: Dynamic SQL - How to convert a varchar2 variable to use as a column name in a select statement [message #634544 is a reply to message #634540] Wed, 11 March 2015 05:00 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

You have to use dynamic SQL or a SQL*Plus variable if this not in a stored procedure and you use SQL*Plus.

[Updated on: Wed, 11 March 2015 05:01]

Report message to a moderator

Re: Dynamic SQL - How to convert a varchar2 variable to use as a column name in a select statement [message #634546 is a reply to message #634540] Wed, 11 March 2015 05:02 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

We had no feedback from you in your previous topics.
Can you update them to give us your final points on them.

Re: Dynamic SQL - How to convert a varchar2 variable to use as a column name in a select statement [message #634549 is a reply to message #634546] Wed, 11 March 2015 05:10 Go to previous messageGo to next message
Adam Brave
Messages: 12
Registered: March 2015
Location: Lisboa
Junior Member
I give up on one of them (sqlhash package) and I'm still working on the other.
Re: Dynamic SQL - How to convert a varchar2 variable to use as a column name in a select statement [message #634551 is a reply to message #634549] Wed, 11 March 2015 05:13 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

OK, then update the one you have given up so we can't continue to work on it.
Although I don't see why this topic is not the answer to the other one.
If you have just a privilege problem then ask your DBA to grant it to you, there is no reason to refuse it if you need it for your work and application.

Re: Dynamic SQL - How to convert a varchar2 variable to use as a column name in a select statement [message #634557 is a reply to message #634551] Wed, 11 March 2015 05:36 Go to previous messageGo to next message
Adam Brave
Messages: 12
Registered: March 2015
Location: Lisboa
Junior Member
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.

Does someone know where is the bug here?

 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 ;
 /
Re: Dynamic SQL - How to convert a varchar2 variable to use as a column name in a select statement [message #634558 is a reply to message #634557] Wed, 11 March 2015 05:41 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Space are missing around "var".
The way to debug dynamic SQL string is to output it using dbms_output before trying to execute it.
Then you have the string the block is trying to execute and you can execute it yourself at SQL prompt and see the errors.
SQL> declare
  2   TYPE pointer IS REF CURSOR;
  3   yo pointer;
  4   var varchar2(20);
  5   query_string varchar2(200);
  6   num number;
  7
  8   begin
  9   var := 'WORKERS'; --should be the column name!
 10   query_string := 'select'||var||'from MY_TABLE where MY_TABLE.ID = 2';
 11
 12  dbms_output.put_line(query_string);  --<-- here
 13
 14   OPEN yo FOR query_string;
 15   LOOP
 16   FETCH yo into num;
 17   EXIT WHEN yo%NOTFOUND;
 18   dbms_output.put_line(num);
 19   END LOOP;
 20   END ;
 21  /
selectWORKERSfrom MY_TABLE where MY_TABLE.ID = 2
declare
*
ERROR at line 1:
ORA-00900: invalid SQL statement
ORA-06512: at line 14


Re: Dynamic SQL - How to convert a varchar2 variable to use as a column name in a select statement [message #634559 is a reply to message #634558] Wed, 11 March 2015 05:56 Go to previous messageGo to next message
Adam Brave
Messages: 12
Registered: March 2015
Location: Lisboa
Junior Member
Nice!! Tkz man!
Re: Dynamic SQL - How to convert a varchar2 variable to use as a column name in a select statement [message #634563 is a reply to message #634557] Wed, 11 March 2015 06:27 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
Answered here http://stackoverflow.com/questions/28982973/dynamic-oracle-sql-how-to-convert-a-varchar2-variable-to-use-as-a-column-name
Re: Dynamic SQL - How to convert a varchar2 variable to use as a column name in a select statement [message #634647 is a reply to message #634563] Thu, 12 March 2015 09:31 Go to previous messageGo to next message
Adam Brave
Messages: 12
Registered: March 2015
Location: Lisboa
Junior Member
I thought the problem was solved but a new and related one arise.
The code above, works only for the column names which represents columns of 'number' type. For the column names that represents columns of Varchar type it gives me a error like:
Error report:
ORA-01722: número inválido
ORA-06512: na linha 12
01722. 00000 -  "invalid number"
*Cause:    
*Action:

why?

[Updated on: Thu, 12 March 2015 09:35]

Report message to a moderator

Re: Dynamic SQL - How to convert a varchar2 variable to use as a column name in a select statement [message #634649 is a reply to message #634647] Thu, 12 March 2015 09:35 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
Adam Brave wrote on Thu, 12 March 2015 07:31
I thought the problem was solved but a new and related one arise.
The code above, works only for the column names which represents columns of 'number' type. For the column names that represents columns of Varchar type it gives me a error like:
Error report:  
ORA-06512: line 12 
01722. 00000 - "invalid number" 
*Cause: 
*Action:


why?



because NUM is datatype NUMBER
>6 num number;
Re: Dynamic SQL - How to convert a varchar2 variable to use as a column name in a select statement [message #634650 is a reply to message #634649] Thu, 12 March 2015 09:53 Go to previous messageGo to next message
Adam Brave
Messages: 12
Registered: March 2015
Location: Lisboa
Junior Member
Jesus...so obvious.
My bad...
Tkz LOL
Re: Dynamic SQL - How to convert a varchar2 variable to use as a column name in a select statement [message #634651 is a reply to message #634650] Thu, 12 March 2015 09:55 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
You can lead some folks to knowledge but you can't make them think.

Please read and follow the forum guidelines, to enable us to help you:

http://www.orafaq.com/forum/t/88153/0/
Re: Dynamic SQL - How to convert a varchar2 variable to use as a column name in a select statement [message #634652 is a reply to message #634651] Thu, 12 March 2015 10:04 Go to previous message
gazzag
Messages: 1118
Registered: November 2010
Location: Bedwas, UK
Senior Member
*despairs at the IT "industry"*
Previous Topic: NO COPY parameters
Next Topic: How to Exclude Locked Rows from Select
Goto Forum:
  


Current Time: Thu Apr 25 11:53:48 CDT 2024