Home » SQL & PL/SQL » SQL & PL/SQL » Display the range wise column names (11.2.0.3)
Display the range wise column names [message #641741] Fri, 21 August 2015 07:24 Go to next message
grpatwari
Messages: 288
Registered: June 2008
Location: Hyderabad
Senior Member
Hi,

I have one requirement in my assignment.

In the database, one of the table contains around 200 columns. I would like to display the range of columns in the query.

For example: Display the columns from 3 to 20 --> display the output from 3rd column to 20th column.

I can provide any range in the query.

Please advice on this.
Re: Display the range wise column names [message #641742 is a reply to message #641741] Fri, 21 August 2015 07:29 Go to previous messageGo to next message
sandeep_orafaq
Messages: 88
Registered: September 2014
Member
The requirement looks strange to me. Why would someone bother about the number of the column?
Even if it is required how about dynamic query?
Re: Display the range wise column names [message #641743 is a reply to message #641742] Fri, 21 August 2015 08:01 Go to previous messageGo to next message
cookiemonster
Messages: 13975
Registered: September 2008
Location: Rainy Manchester
Senior Member
You need either
a) dynamic SQL (execute immediate, dbms_sql or ref cursor)
b) Front end code in whatever language that constructs a select statement on the fly with the correct column list (so dynamic SQL outside the DB).
Re: Display the range wise column names [message #641747 is a reply to message #641741] Fri, 21 August 2015 09:27 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
grpatwari wrote on Fri, 21 August 2015 05:24
Hi,

I have one requirement in my assignment.

In the database, one of the table contains around 200 columns. I would like to display the range of columns in the query.

For example: Display the columns from 3 to 20 --> display the output from 3rd column to 20th column.

I can provide any range in the query.

Please advice on this.


More than likely table "design" does NOT conform to Third Normal Form.
Table data is stored in blocks which mean whole rows are returned regardless of which columns are to be presented.
So SELECT every column & then display only those you desire in the User Interface
Re: Display the range wise column names [message #641790 is a reply to message #641747] Mon, 24 August 2015 05:06 Go to previous messageGo to next message
grpatwari
Messages: 288
Registered: June 2008
Location: Hyderabad
Senior Member
Yes. I require the dynamic SQL which I need to run in SQL.
Re: Display the range wise column names [message #641792 is a reply to message #641790] Mon, 24 August 2015 05:19 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

What is your client tool?


Re: Display the range wise column names [message #641814 is a reply to message #641792] Mon, 24 August 2015 23:40 Go to previous messageGo to next message
grpatwari
Messages: 288
Registered: June 2008
Location: Hyderabad
Senior Member
SQL Developer.
Re: Display the range wise column names [message #641816 is a reply to message #641814] Tue, 25 August 2015 00:12 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

Then I can't help you.
Note there is no pure SQL solution.

Re: Display the range wise column names [message #642548 is a reply to message #641816] Mon, 14 September 2015 05:46 Go to previous messageGo to next message
aspire
Messages: 18
Registered: September 2015
Location: TVN
Junior Member
As of my knowledge i have created one procedure, that will help to return values dynamically...
Just pass table_name ,rows from&to, column from&to
create or replace PROCEDURE PRC_BALA(LV_rEF OUT SYS_REFCURSOR,LV_TABLE_NAME VARCHAR2,ROWS_FROM NUMBER,ROWS_TO NUMBER,COL_RANGE_FROM NUMBER,COL_RANGE_TO NUMBER)  IS
LV_QUERY VARCHAR2(9000);
COLUMN_NM VARCHAR2(2000) :=NULL;
ORG_COLUMN_NM  VARCHAR2(2000) :=NULL;
CURSOR C1 IS SELECT COLUMN_NAME,ROWNUM,ROWNO 
               FROM (
                     SELECT COLUMN_NAME,ROWNUM,ROW_NUMBER() OVER(ORDER BY COLUMN_NAME) ROWNO 
                       FROM USER_TAB_COLUMNS 
                      WHERE TABLE_NAME=UPPER(LV_TABLE_NAME) 
                   ORDER BY COLUMN_NAME ASC)
              WHERE ROWNO BETWEEN COL_RANGE_FROM AND COL_RANGE_TO;
BEGIN
  FOR I IN C1 LOOP
    COLUMN_NM :=COLUMN_NM||','||I.COLUMN_NAME ; 
  END LOOP;
  ORG_COLUMN_NM :=  SUBSTR(COLUMN_NM,2)||',';
  LV_QUERY := 'SELECT '||ORG_COLUMN_NM||' ROWNO FROM (
  SELECT '||ORG_COLUMN_NM||' ROW_NUMBER() OVER(ORDER BY ROWNUM) ROWNO  FROM '||LV_TABLE_NAME ||') WHERE ROWNO BETWEEN '||ROWS_FROM||' AND '||ROWS_TO; 
  OPEN LV_rEF FOR LV_QUERY;
END;

Re: Display the range wise column names [message #642549 is a reply to message #642548] Mon, 14 September 2015 06:01 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

Quote:
ORDER BY COLUMN_NAME ASC


This is wrong. The columns are not alphabetically ordered in a table.

Quote:
ROW_NUMBER() OVER(ORDER BY ROWNUM)


This is silly.
What do you think it does?

In addition, you add pointless rows_from and rows_to OP did not ask for.
Why do you complexify the question for nothing?

Re: Display the range wise column names [message #642550 is a reply to message #642549] Mon, 14 September 2015 06:12 Go to previous messageGo to next message
aspire
Messages: 18
Registered: September 2015
Location: TVN
Junior Member
create or replace PROCEDURE PRC_BALA(LV_rEF OUT SYS_REFCURSOR,LV_TABLE_NAME VARCHAR2,COL_RANGE_FROM NUMBER,COL_RANGE_TO NUMBER)  IS
LV_QUERY VARCHAR2(9000);
COLUMN_NM VARCHAR2(2000) :=NULL;
ORG_COLUMN_NM  VARCHAR2(2000) :=NULL;
CURSOR C1 IS SELECT COLUMN_NAME,ROWNO 
               FROM (
                     SELECT COLUMN_NAME,ROW_NUMBER() OVER(ORDER BY COLUMN_NAME) ROWNO 
                       FROM USER_TAB_COLUMNS 
                      WHERE TABLE_NAME=UPPER(LV_TABLE_NAME) 
                   )
              WHERE ROWNO BETWEEN COL_RANGE_FROM AND COL_RANGE_TO;
BEGIN
  FOR I IN C1 LOOP
    COLUMN_NM :=COLUMN_NM||','||I.COLUMN_NAME ; 
  END LOOP;
  ORG_COLUMN_NM :=  SUBSTR(COLUMN_NM,2);
  LV_QUERY := 'SELECT '||ORG_COLUMN_NM||' FROM (
  SELECT '||ORG_COLUMN_NM||'  FROM '||LV_TABLE_NAME ||')'; 
  OPEN LV_rEF FOR LV_QUERY;
END;

[Updated on: Mon, 14 September 2015 06:15]

Report message to a moderator

Re: Display the range wise column names [message #642556 is a reply to message #642550] Mon, 14 September 2015 06:26 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

Still wrong.

Re: Display the range wise column names [message #642557 is a reply to message #642556] Mon, 14 September 2015 06:28 Go to previous messageGo to next message
aspire
Messages: 18
Registered: September 2015
Location: TVN
Junior Member
Might be i am wrong..pls explain me where you are finding errors
Re: Display the range wise column names [message #642560 is a reply to message #642557] Mon, 14 September 2015 06:29 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

It is my first remark in my previous post.

Re: Display the range wise column names [message #642561 is a reply to message #642560] Mon, 14 September 2015 07:16 Go to previous message
cookiemonster
Messages: 13975
Registered: September 2008
Location: Rainy Manchester
Senior Member
user_tab_columns has a column called column_id - have a look at it
Previous Topic: String Comparasition
Next Topic: except duplicate record
Goto Forum:
  


Current Time: Tue Jul 28 22:25:19 CDT 2026