简体   繁体   English

SQL PLUS 或 SQL 开发人员中的映射值

[英]Mapped values in SQL PLUS or SQL Developer

I have a table with a column that contains integers.我有一个包含整数列的表。 Those integers refer to a C enum.这些整数指的是 C 枚举。

It would be great to be able to map the numbers to the enum in SQL Plus and/or SQL Developer.如果能够在 SQL Plus 和/或 SQL 开发人员中将数字 map 输入到枚举中,那就太好了。

I think I need to use the column command, but haven't figured out exactly how.我想我需要使用 column 命令,但还没有弄清楚到底如何。

For example, if I have this query:例如,如果我有这个查询:

SELECT * FROM employees

I would get something like this:我会得到这样的东西:

name姓名 department
john doe约翰·多伊 2 2
jane doe简·多伊 5 5

Then in some cpp file I would see that 2 is sales, 5 is support, etc.然后在一些 cpp 文件中,我会看到 2 是销售,5 是支持,等等。

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:能够在我的 session 开头执行一些命令会很棒,所以当我执行查询时,我得到了这个:

name姓名 department
john doe约翰·多伊 sales销售量
jane doe简·多伊 support支持

I don't think that it exists in Oracle.我不认为它存在于 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.如果我错了并且enum确实存在,我想看看它。

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:如果您很懒(并且不想每次都输入),请创建一个视图,然后从视图中创建 select:

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>

I ended up using an invisible virtual column.我最终使用了一个不可见的虚拟列。

I created a little program that loops through the enum an generates the SQL:我创建了一个循环遍历枚举并生成 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.我仍然需要来回切换可见性,因为 SQL Developer 不会显示不可见的列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM