Home » SQL & PL/SQL » SQL & PL/SQL » How to retrieve columns by its order
How to retrieve columns by its order [message #193613] Mon, 18 September 2006 08:15 Go to next message
mudalimuthu
Messages: 64
Registered: May 2005
Location: Bangalore
Member
I have empTable which contains the following 3 columns
1. empname
2. empcode
3. age

I want to retrieve the table by its order number

ex:

My query is something like the following

select 2 from emptable

Output should be like this:

Empcode
-------
xyz
yyy
zzz
Re: How to retrieve columns by its order [message #193617 is a reply to message #193613] Mon, 18 September 2006 08:38 Go to previous messageGo to next message
JRowbottom
Messages: 5933
Registered: June 2006
Location: Sunny North Yorkshire, ho...
Senior Member
Assuming you want the data back in Empcode order, try:

SELECT empcode
FROM   emptable
ORDER BY empcode
Re: How to retrieve columns by its order [message #193634 is a reply to message #193613] Mon, 18 September 2006 10:51 Go to previous message
shoblock
Messages: 325
Registered: April 2004
Senior Member
seems that you want DYNAMIC SQL. use USER_TAB_COLUMNS to find out what column name corresponds to COLUMN_ID of 2, then build the query and execute it. since you posted this under SQL instead of PL/SQL, I'll show you the sqlplus approach:

column cn new_value my_col
select column_name cn from user_tab_Columns
where table_name='EMPTABLE' and column_id=2;

select &my_col from emptable order by 1;
Previous Topic: Trigger Question - How to insert multiple rows?
Next Topic: Converting a SQL Server stored procedure to Oracle
Goto Forum:
  


Current Time: Wed Dec 07 03:13:17 CST 2016

Total time taken to generate the page: 0.11355 seconds