1

I have a few tables with more than 30 colums and I need to select them all in my application sometimes with aliases on colums.

The synthax "Select column1 as columnName, column2 as ColumnName2, ......." is pretty heavy with all those columns.

Is it possible to give names to my columns with a select * ?

user272735
  • 10,473
  • 9
  • 65
  • 96
Sideness
  • 123
  • 1
  • 11
  • 1
    No I don't think that is possible. – Mahesh Mar 02 '15 at 10:53
  • 1
    No. You give an alias to a single column – Joe Taras Mar 02 '15 at 10:55
  • what do you exactly mean? To write select like that or to have it? Is automatic generation enough for you? – smnbbrv Mar 02 '15 at 10:55
  • Well, in my VB.net app I need to select all columns from some tables to work with it. And I would like every column to have an alias. I was wondering if it is possible to do something like "Select * As name1, name2, name3 from ...". The code would be less heavy I suppose. But it isn't possible apparently ! Thank you anyway :) I'm not very familiar with StackOverflow's rules. Do I need to delete the topic or should I leave it like that ? – Sideness Mar 02 '15 at 11:00
  • 3
    Almost a duplicate of [this](http://stackoverflow.com/q/3833038/266304). Using `select *` is considered bad practice anyway, in this case because even if your application needs all the columns then it is relying on them always being in the same order in the data dictionary - which may break one day. – Alex Poole Mar 02 '15 at 11:12
  • Thank you for your comment @AlexPoole, I didn't know that. Very instructive. – Sideness Mar 02 '15 at 12:23
  • Sometimes with great power of SQL come a great amount of typing. You'll get used to it. – user272735 Mar 03 '15 at 08:36

1 Answers1

2

sometimes with aliases on colums.

If this is not a frequent requirement, and that only sometimes your application requires to have column aliases, then create a VIEW. Use the view instead of the table whenever you need different column names, i.e. whenever you need the aliases.

For example,

SQL> SELECT empno, deptno, sal FROM emp WHERE ROWNUM <= 5;

     EMPNO     DEPTNO        SAL
---------- ---------- ----------
      7369         20        800
      7499         30       1600
      7521         30       1250
      7566         20       2975
      7654         30       1250

SQL>
SQL> CREATE OR REPLACE VIEW emp_view AS
  2  SELECT empno AS employee_id,
  3  deptno AS department_no,
  4  sal AS salary
  5  FROM emp;

View created.

SQL>
SQL> SELECT * FROM emp_view WHERE ROWNUM <=5;

EMPLOYEE_ID DEPARTMENT_NO     SALARY
----------- ------------- ----------
       7369            20        800
       7499            30       1600
       7521            30       1250
       7566            20       2975
       7654            30       1250

SQL>
Lalit Kumar B
  • 47,486
  • 13
  • 97
  • 124
  • Well, it was easier than I thought ! I don't why I didn't think about it earlier. Thank you for your answer. – Sideness Mar 02 '15 at 12:22
  • 1
    You're welcome. It is a one time activity to write a view, and use it later. Thus, not changing the table structure. – Lalit Kumar B Mar 02 '15 at 12:23