Solved

How to exclude one column from select statement?

Posted on 2011-02-18
14
1,506 Views
Last Modified: 2012-05-11
Have a table with 100 columns and want to show 99 columns. Easy way is
select col1, col2, ...col99
from t1;
That will be multiple pages only for select part. but if doing the following
select * from t1;
That will show everything.
Gurus, Is there any easy way to solve my issue?


0
Comment
Question by:jl66
  • 4
  • 3
  • 3
  • +2
14 Comments
 
LVL 77

Assisted Solution

by:slightwv (䄆 Netminder)
slightwv (䄆 Netminder) earned 260 total points
ID: 34927472
In sqlplus it's pretty easy:

column col100 noprint

Where are you running the query?
0
 
LVL 14

Expert Comment

by:VBClassicGuy
ID: 34929314
You could either:

1. Select all fields and just ignore the unwanted one in code.

2. Build a view which selects all columns except the unwanted one, and then select from the view rather than the table.
0
 

Author Comment

by:jl66
ID: 34929340
Thank you so much. The point should be awarded to you now. However, the purpose for it is as follows:

create table t2 as
select * from t1;

in t2 it only needs col1....col99. Is there any way to deal with it?
If you need a new ticket, I can open a new one. Sorry for that.

0
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 

Author Comment

by:jl66
ID: 34929363
=>Is there any way to deal with it?  It means that "Don't list everything col1,..col99 in select part".
0
 
LVL 77

Accepted Solution

by:
slightwv (䄆 Netminder) earned 260 total points
ID: 34929380
Selecting in code and create table as select are really two different things.

I don't know of an easy way to do this.

You never did respond to my question:
Where are you running the query?

You can likely 'build' the query dynamically but we need to know your exact requirements.
0
 
LVL 14

Expert Comment

by:VBClassicGuy
ID: 34929435
If you create the view I mentioned, you can use:

CREATE TABLE t2 SELECT * FROM vwT1

(assuming you name your view vwT1)
0
 
LVL 14

Assisted Solution

by:VBClassicGuy
VBClassicGuy earned 100 total points
ID: 34929464
Or, go ahead and use your statement of:

CREATE TABLE t2 AS SELECT * FROM t1

then follow it with this statement:

ALTER TABLE t2 DROP COLUMN MyUndesiredField
0
 

Author Comment

by:jl66
ID: 34931265
If the table has over 100 Millions rows, adding one column and dropping it would be costly. Is there any better way to deal with it
0
 
LVL 15

Expert Comment

by:Aaron Shilo
ID: 34931830
hi

no need to drop the column.
use the UNUSED command

alter table alter column set unused.

this is a Datadict command and works immediatly no overhead.
0
 
LVL 15

Assisted Solution

by:Aaron Shilo
Aaron Shilo earned 140 total points
ID: 34931834
^

just a fix for the command

ALTER TABLE table_name SET UNUSED (column_name);
0
 
LVL 15

Assisted Solution

by:Aaron Shilo
Aaron Shilo earned 140 total points
ID: 34931836
hi

there is another way.

you could generate a dynamic query using  DBA_TAB_COLUMNS
and select the first 99 columns for the dynamic query.
0
 
LVL 1

Expert Comment

by:maheshchari
ID: 34933789
select * from tablename where rownum>=1 and rownum<=99;
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 34934337
What are you wanting to do?

Are you wanting to select columns or remove the column permanently from the table?

ashilo,

I already mentioned dynamic sql but thanks for providing the view.
0
 

Author Closing Comment

by:jl66
ID: 34934876
Thank for everyone. Dynamically build the query may be the practical way to work out the issue. Special thanks to slightwv and ashilo. It does not seem matter. I can use dba_tab_columns or user_..., depending on the user privilege logged in to form the query.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question