I have a table with a column that contains integers. Those integers refer to a C enum.
It would be great to be able to map the numbers to the enum in SQL Plus and/or SQL Developer.
I think I need to use the column command, but haven't figured out exactly how.
For example, if I have this query:
SELECT * FROM employees
I would get something like this:
| name | department |
|---|---|
| john doe | 2 |
| jane doe | 5 |
Then in some cpp file I would see that 2 is sales, 5 is support, etc.
It would be great to be able execute some command at the beginning of my session, so when I do the query I get this instead:
| name | department |
|---|---|
| john doe | sales |
| jane doe | support |
CodePudding user response:
I don't think that it exists in Oracle. What we usually do is to join two (normalized) tables, possibly referenced to each other via foreign key constraints. If I'm wrong and enum actually exists, I'd like to see it.
Therefore: sample tables:
SQL> create table employees as
2 select 'john doe' name, 2 department from dual union all
3 select 'jane doe', 5 from dual;
Table created.
SQL> create table departments as
2 select 2 department, 'sales' name from dual union all
3 select 5, 'support' from dual;
Table created.
SQL> select * From employees;
NAME DEPARTMENT
-------- ----------
john doe 2
jane doe 5
SQL> select * from departments;
DEPARTMENT NAME
---------- -------
2 sales
5 support
This is what we normally do:
SQL> select e.name, d.name department
2 from employees e join departments d on d.department = e.department;
NAME DEPARTM
-------- -------
john doe sales
jane doe support
If you're lazy (and don't want to type it every time), create a view and then select from the view:
SQL> create or replace view v_employees as
2 select e.name, d.name department
3 from employees e join departments d on d.department = e.department;
View created.
SQL> select * From v_employees;
NAME DEPARTM
-------- -------
john doe sales
jane doe support
SQL>
CodePudding user response:
I ended up using an invisible virtual column.
I created a little program that loops through the enum an generates the SQL:
ALTER TABLE employees ADD departmentLbl INVISIBLE AS CASE
WHEN department = 2 THEN 'sales'
WHEN department = 5 THEN 'support'
ELSE TO_CHAR(department)
I still need to toggle the visibility back and forth because SQL Developer won't show invisible columns.
