简体   繁体   English

使用Oracle SQL Developer中的表达式对多个列进行别名

[英]Aliasing multiple columns using an expression in Oracle SQL Developer

In Oracle SQL Developer, is it possible to alias multiple column names as part of a SELECT statement using an expression (as opposed to manually specifying the aliases for each column)? 在Oracle SQL Developer中,是否可以使用表达式将多个列名作为SELECT语句的一部分(而不是手动指定每列的别名)?

Specifically, I have a mapping table that stores the task-relevant subset of columns from a large data table. 具体来说,我有一个映射表,用于存储大型数据表中与任务相关的列子集。 Each entry in the mapping table ties a data table column name to a human readable description. 映射表中的每个条目都将数据表列名称与人类可读的描述相关联。 I want to select the data table columns listed in the mapping table and display them with the mapping table descriptions as the column headers, but WITHOUT manually typing in the column names and their human-readable aliases one-by-one. 我想选择映射表中列出的数据表列,并将它们与映射表描述一起显示为列标题,但是无需手动逐一键入列名及其易于理解的别名。 Is this possible? 这可能吗?

The closest I've found to an answer online is this SO question which suggests what I want to do is NOT possible: Oracle rename columns from select automatically? 我在网上找到答案的最接近的是这个问题,它表明我想做的事情是不可能的: Oracle会自动重命名列中的列?

But, that question is from 2010. I'm hoping the situation has changed. 但是,这个问题是从2010年开始的。我希望情况有所改变。 Thank you for your help. 谢谢您的帮助。

This still cannot be done with 100% native SQL. 使用100%本机SQL仍然无法做到这一点。 These overly-dynamic situations are usually best avoided; 通常最好避免这些过度动态的情况; a little extra typing is generally better than adding complicated code. 一点额外打字通常比添加复杂的代码更好。

If you truly have an exceptional case and are willing to pay the price there is a way to do this. 如果你真的有一个特殊的情况,并愿意支付的价格有办法做到这一点。 It doesn't use 100% natural SQL, but it could be considered "pure" SQL since it uses the Oracle Data Cartridge framework to extend the database. 它不使用100%自然SQL,但它可以被认为是“纯”SQL,因为它使用Oracle Data Cartridge框架来扩展数据库。

You can use my open source project Method4 to run dynamic SQL in SQL. 您可以使用我的开源项目Method4在SQL中运行动态SQL。 Follow the Github steps to download and install the objects. 按照Github步骤下载并安装对象。 The code is painfully complicated but luckily you won't need to understand most of it. 代码非常复杂,但幸运的是,您不需要了解其中的大部分内容。 Only the simple changes below are necessary to get started on customizing column names. 只有以下简单的更改才可以开始自定义列名称。

Method4 Changes 方法4的变化

Create a variable to hold the new column name. 创建一个变量来保存新的列名。 Add it to the declaration section of the function ODCITableDescribe, on line 12 of the file METHOD4_OT.TPB. 将其添加到函数ODCITableDescribe的声明部分,位于文件METHOD4_OT.TPB的第12行。

  v_new_column_name varchar2(32);

Create a SQL statement to map the old column to the new column. 创建一个SQL语句以将旧列映射到新列。 Add this to line 31, where it will be run for each column. 将其添加到第31行,将在每列运行该行。

     --Get mapped column name if it exists.  If none, use the existing name.
     select nvl(max(target_column_name), r_sql.description(i).col_name)
     into v_new_column_name
     from column_names
     where source_column_name = r_sql.description(i).col_name;

Change line 42 to refer to the new variable name: 更改第42行以引用新变量名称:

                      substr(v_new_column_name, 1, 30),

Mapping Table 映射表

drop table column_names;
create table column_names
(
    source_column_name varchar2(30),
    target_column_name varchar2(30),
    constraint column_names_pk primary key(source_column_name)
);
insert into column_names values('A1234', 'BETTER_COLUMN_NAME');
insert into column_names values('B4321', 'Case sensitive column name.');

Query Example 查询范例

Now the column names from any query can magically change to whatever values you want. 现在,任何查询的列名称都可以神奇地更改为所需的任何值。 And this doesn't simply use text replacement; 这不仅仅是使用文本替换; the columns from a * will also change. a *的列也会改变。

SQL> select * from table(method4.query('select 1 a1234, 2 b4321, 3 c from dual'));

BETTER_COLUMN_NAME Case sensitive column name.          C
------------------ --------------------------- ----------
                 1                           2          3

Warnings 警告

Oracle SQL is horrendously complicated and any attempt to build a layer on top if it has many potential problems. Oracle SQL非常复杂,如果有很多潜在的问题,那么任何尝试构建一个层都是最重要的。 For example, performance will certainly be slower. 例如,性能肯定会变慢。 Although I've created many unit tests I'm sure there are some weird data types that won't work correctly. 虽然我已经创建了许多单元测试,但我确信有些奇怪的数据类型无法正常工作。 (But if you find any, please create a Github issue so I can fix it.) (但如果你发现任何问题,请创建一个Github问题,以便我可以修复它。)

In my experience, when people ask for this type of dynamic behavior, it's usually not worth the cost. 根据我的经验,当人们要求这种动态行为时,通常不值得花费。 Sometimes a little extra typing is the best solution. 有时,进行一些额外的输入是最好的解决方案。

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

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