Skip to Main Content
  • Questions
  • How to refer to a column in select list not by its name?

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, duan.

Asked: May 01, 2007 - 2:45 pm UTC

Last updated: September 23, 2024 - 1:01 pm UTC

Version: 9.2.0

Viewed 100K+ times! This question is

You Asked

Hi Tom,

Can I use column number instead of name in a select list? Because I have a list of tables need to access by the first column in a loop. The first column names are different. I like to use the same select statement for each table. is it doable? If yes, could you show me the syntax or direct me to some docomentation?

Thanks.

and Tom said...

Nope.

They are all accessing different tables - hence, you could not possibly use the same sql statement!!!


And you must refer to them by name in the query.

Seems very strange to have a generic process that must access the "first column" of a table, very very very strange indeed. So strange - I don't even want to know why...

Rating

  (3 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Kirill, May 02, 2007 - 3:22 pm UTC

You can generate your sql's dynamically:

select 'select ' || column_name || ' from ' || table_name || ';'
from user_tab_columns
where column_id = 1;

Ok I don't have a solution but I do have a use-case

Agrapha, September 19, 2024 - 7:36 am UTC

Suppose I have a table with like 148 columns. It also has about 24 million rows. I'm trying to use a spool command to write out a CSV to a local disk folder.

My select input fails for "SF2-0027: Input is too long (>2499 characters) - line ignored"

I inherited this oracle database 11G so the elaborate column names are not my doing.
Chris Saxon
September 19, 2024 - 9:57 am UTC

I'm not sure how this helps? You just need to put some linebreaks in your statement https://asktom.oracle.com/ords/f?p=100:11:0::::P11_QUESTION_ID:4789399600346029472

Macro for Initial question

Rajeshwaran, Jeyabal, September 21, 2024 - 3:43 pm UTC

Was reading the "Initial" question and it sounds to be better use case for SQL Macro, in the latest generation of database.
demo@FREEPDB1> create or replace function foo(
  2     p_tab dbms_tf.table_t
  3     , p_col_num number default 1 )
  4  return varchar2
  5  sql_macro as
  6     l_col varchar2(30);
  7  begin
  8     l_col := p_tab.column(p_col_num).description.name;
  9     return ' select '||l_col||' from p_tab ' ;
 10  end;
 11  /

Function created.

demo@FREEPDB1> select * from foo(DEPT);

    DEPTNO
----------
        10
        20
        30
        40

demo@FREEPDB1> select * from foo(DEPT,2);

DNAME
--------------
ACCOUNTING
RESEARCH
SALES
OPERATIONS

demo@FREEPDB1>

Chris Saxon
September 23, 2024 - 1:01 pm UTC

You can, though you have to use literals for the column number. Bind variables won't work because they're null when parsing the macro:

var col number;
exec :col := 2;
with rws as ( 
  select * from hr.employees natural join hr.departments 
)
select * from foo ( rws, :col ) ;

ORA-62565: The SQL Macro method failed with error(s).
ORA-06502: PL/SQL: numeric or value error: NULL index table key value