Display Mth to Nth Column in a Table

How to display data in the table between mth column to nth column in a table in Oracle?

Questions by bharaththiruveedula

Showing Answers 1 - 1 of 1 Answers

Hi, I don't think there exists predefined pseudo column for column number in oracle. I have done a procedure, which creates a view for displaying data from Mth to Nth column in a table. Hope it helps.

Code
  1.  

  2. CREATE OR REPLACE PROCEDURE select_col(first_col NUMBER, last_col NUMBER)



  3.  


  4. col_no NUMBER;

  5. col_name VARCHAR2(20);

  6. selected_fields VARCHAR2(200);

  7.  

  8. -- USER_TAB_COLUMNS -> contains Columns of user's tables, views and clusters

  9. -- create a cursor, that has all the column names and it's corresponding column number.

  10.  

  11. CURSOR col_cursor IS

  12. SELECT ROWNUM,COLUMN_NAME FROM USER_TAB_COLUMNS WHERE TABLE_NAME=UPPER('table_name') ;

  13.  


  14. OPEN col_cursor;

  15.  


  16.  

  17. FETCH col_cursor INTO col_no,col_name;

  18. EXIT WHEN col_cursor%NOTFOUND;

  19.  

  20. -- Fetch the columns that's between the given criteria

  21.  

  22. IF(col_no=first_col) THEN

  23.    selected_fields:= col_name;


  24.  

  25. IF(col_no>first_col) AND (col_no<=last_col) THEN

  26.    selected_fields := selected_fields ||','|| col_name;

  27.    DBMS_OUTPUT.PUT_LINE(selected_fields);

  28. -- Create a view to display the data between the columns.

  29.    EXECUTE IMMEDIATE 'CREATE OR REPLACE VIEW myview AS SELECT '||selected_fields||' FROM table_name';


  30.  


  31. CLOSE col_cursor;

  32.  



  33.  

  34. -------------------------------------------------------------------------------------------------------------------------

  35. -- specify the column numbers to display the data in between.


  36. select_col(2,4);


  37.  

  38. -------------------------------------------------------------------------------------------------------------------------

  39.  

  40. SELECT * FROM myview;

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions