Home » SQL & PL/SQL » SQL & PL/SQL » Select table.column(i) from table !
Select table.column(i) from table ! [message #9039] Tue, 14 October 2003 19:17 Go to next message
Alex
Messages: 190
Registered: August 1999
Senior Member
Hi this should be a really quick yes or no!

And i think the answer is no..

Can someone confirm if you can retrive a column by its 'column number' rather than it column name?

e.g.

select t.column(1) from table_name t;

would return values for the first column in table 'table_name'.

thanks.
Re: Select table.column(i) from table ! [message #9044 is a reply to message #9039] Wed, 15 October 2003 06:44 Go to previous messageGo to next message
Thiru
Messages: 1089
Registered: May 2002
Senior Member
Alex
there is no 'fixed' column order in a normal table. Columns are usually stored in the blocks as per the CREATE TABLE statement but no guarentee again..You can reorg the table that could affect the column order.

However you can sort by column position ..like

thiru@9.2.0:SQL>select x,y from t order by 2;

X Y
---------- ----------
2 Ed
1 John
3 Mary

3 rows selected.

thiru@9.2.0:SQL>select x,y from t order by y;

X Y
---------- ----------
2 Ed
1 John
3 Mary

3 rows selected.

are same(ie orderring by 2nd column in the Select clause).
Collections may allow you to use similar subscripts but not in the fashion you are expecting..

-Thiru
Re: Select table.column(i) from table ! [message #9045 is a reply to message #9039] Wed, 15 October 2003 06:46 Go to previous messageGo to next message
Art Metzer
Messages: 2480
Registered: December 2002
Senior Member
It's possible, but inadvisable. Your code should not care what order the columns are in. Plus, how are you going to account for different return types (VARCHAR2, NUMBER, DATE, CLOB, etc., etc.)? Plus, what if you pass in a column number outside the range of the table? Plus, how are you going to keep your rows straight between calls (what can you ORDER BY)?

Did I mention that I think this is a bad idea?
SQL> CREATE OR REPLACE PROCEDURE get_column_value (
  2      p_schema        IN  VARCHAR2
  3  ,   p_table_name    IN  VARCHAR2
  4  ,   p_column_number IN  NUMBER
  5  )
  6  IS
  7      l_column_name       VARCHAR2(30);
  8      l_sql               VARCHAR2(113) := 'SELECT TO_CHAR(';
  9      TYPE l_rc           IS REF CURSOR;
 10      c_cursor            l_rc;
 11      l_column_value      VARCHAR2(255);
 12  BEGIN
 13      SELECT dtc.column_name
 14      INTO   l_column_name
 15      FROM   sys.dba_tab_columns      dtc
 16      WHERE  dtc.owner      = UPPER(p_schema)
 17      AND    dtc.table_name = UPPER(p_table_name)
 18      AND    dtc.column_id  = p_column_number;
 19  
 20      l_sql := l_sql
 21               || l_column_name
 22               || ') FROM '
 23               || p_schema
 24               || '.'
 25               || p_table_name;
 26               
 27      OPEN c_cursor FOR l_sql;
 28      LOOP
 29          FETCH c_cursor INTO l_column_value;
 30          EXIT WHEN c_cursor%NOTFOUND;
 31          DBMS_OUTPUT.PUT_LINE(l_column_value);
 32      END LOOP;
 33  END get_column_value;
 34  /
  
Procedure created.
  
SQL> EXEC get_column_value('SCOTT','EMP',1);
7369
7499
7521
7566
7654
7698
7782
7788
7839
7844
7876
7900
7902
7934
  
PL/SQL procedure successfully completed.
  
SQL> EXEC get_column_value('SCOTT','DEPT',2);
ACCOUNTING
RESEARCH
SALES
OPERATIONS
  
PL/SQL procedure successfully completed.
  
SQL> EXEC get_column_value('SCOTT','EMP', 50000);
BEGIN get_column_value('SCOTT','EMP', 50000); END;
  
*
ERROR at line 1:
ORA-01403: no data found
ORA-06512: at "SCOTT.GET_COLUMN_VALUE", line 13
ORA-06512: at line 1
  
  
SQL> 
A.
Re: Select table.column(i) from table ! [message #9053 is a reply to message #9045] Wed, 15 October 2003 17:37 Go to previous message
Alex
Messages: 190
Registered: August 1999
Senior Member
Thanks A!

Yes.. we certainly agree that it is a bad idea!

We are just building a dynamic query builder for a given and known data model... it was just an element of an approach we were considering.
Previous Topic: Looking for a kind of outer-join/BETWEEN....
Next Topic: First Cap
Goto Forum:
  


Current Time: Thu Apr 25 00:39:30 CDT 2024