Is it possible, in PLSQL, to select all of the fields in a table except for 1 or 2, without having to specify the fields you want?
Example, the employee table has the fields:
- id
- firstname
- lastname
- hobbies
Is it still possible to write a query similar to
select * from employee
while leaving the field hobbies
without with having to write something like this?
select id, firstname, lastname from employee
to create view :-
create view view_name as select id,first_name,last_name from employee where id in ('','','')
note:- this is like virtual table in your database but , it can effect values in actual table.
Are you running on Oracle 12c?
If so, consider whether this meets your needs:
In that case column
undesired_col_name
will be completely usable, but it will be excluded from anySELECT *
statements and the like (e.g.,%ROWTYPE
) as though it didn't exist.An old thread but, yes... there is a way to do it in Oracle:
To understand how the PIVOT works and why it solves the question, lets take a better example for our
employee
sample table:The result here is:
The exact same output can be achieved using this easier to understand query:
So, the column
hobbies
is never selected, just as the columnid
, both specified inside the PIVOT clause. All other columns are grouped and selected.Well, returning to the first query, it works for two reasons:
1- you will not lose any row in the grouping process because the id column is unique and no columns were specified for aggregations;
2- as the pivot generates N * M new columns, where N = number of values of the IN clause and M = number of aggregations specified, so having no filters and that single harmless aggregation will produce 0 * 1 = 0 new columns and will remove the ones specified in the PIVOT clause, which is just the hobbies.
ANSWER TO COMMENT 1
The first line of this question says: "... without having to specify the fields you want". In all other answers the proposed queries specifies the desired fields in the SELECT clause, except in mine, actually.
Also, in the question title says "... without writer's cramp". Well, what's the correct measure to identify a writer's cramp? My best effort would be to foresee a good SQL standard to this problem and compare with my answer. Actually, I think this "standard" could be something like SELECT * NOT IN ([col1], [col2], ...).
Now, I can see in both queries:
It means that you need to write a bit more in my approach as you need a fake aggregation and the PIVOT clause... but it's really few characters more...