在Oracle SQL查询的结果中包括列名吗? [英] Include column names in the results of an Oracle SQL query?

查看:83
本文介绍了在Oracle SQL查询的结果中包括列名吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在基于Oracle数据库的软件工具中输入SQL查询(SELECT ...).

I am typing in SQL queries (SELECT ...) in a software tool which is based on an Oracle database.

现在,我想在结果数据的第一行中包含各列的名称.否则,在导出时不包括在内,我还没有找到任何允许后者的选项.

Now, I would like to have in the first row of the resulting data the names of the columns. Otherwise, on exporting they are not included and I haven't found any option which would allow the latter.

有人知道如何在SQL查询的结果中包括列名吗?

Does anyone have an idea how to include column names in the results of an SQL query?

我知道,如果该列具有类型值(例如Integer),则会出现一些问题,但随后我可能会简单地(如何?)将所有内容转换为字符串.

I understand that there would be some problems if the column has values of type, say, Integer, but then I would probably transform everything simply (how?) to strings.

推荐答案

这将是一种快速而肮脏的方法,可以用来完成您想要的操作.如果列是非varchar2类型,则需要将第二个SELECT结果转换为VARCHAR2.

This would be a quick and dirty method for doing what you want. If a column was a non-varchar2 type then you'd need to cast the second SELECT result to VARCHAR2.

有关转换语法,请参见 TO_CHAR .

See TO_CHAR for the conversion syntax.

SELECT 'colname1',
       'colname2',
       'colname3',
       1 AS ordering
  FROM dual
UNION
SELECT colname1,
       colname2,
       colname3,
       2 AS ordering
  FROM yourtable
ORDER BY ordering;

如果您真的不想看到排序"列,则可以将整个语句包装在另一个选择中,该选择仅选择要查看的列.

If you really don't want to see the "ordering" column then you could wrap the whole statement in another select that just selects the columns you want to see.

尽管我必须同意这些评论,但理想情况下,您应该能够通过IDE工具而不是变通方法来导出元数据.

I'd have to agree with the comments though, you should ideally be able to export metadata through your IDE tool rather than a workaround.

希望有帮助...

这篇关于在Oracle SQL查询的结果中包括列名吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆