Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle SQL - Selecting column without knowing column name

Is it possible to select the value of the second column using something like an index or column position if i dont know the name of the column?

Select col(2) FROM (
    Select 'a', 'b',' c', 'd' from dual
)
like image 296
ziggy Avatar asked Jun 10 '26 15:06

ziggy


1 Answers

Is it possible? Sure. You could write a PL/SQL block that used dbms_sql to open a cursor using the actual query against dual, describe the results, bind a variable to whatever you find the second column to be, fetch from the cursor, and then loop. That would be a terribly involved and generally rather painful process but it could be done.

The SQL language does not define a way to do this in a static SQL statement and Oracle does not provide an extension that would allow this. I'd be rather concerned about the underlying problem that you're trying to solve, though, if you somehow know that you want the second column but don't know what that column represents. That's not something that makes a lot of sense in a relational database.

like image 164
Justin Cave Avatar answered Jun 13 '26 15:06

Justin Cave