在Oracle SQL查询的结果中包含列名?

时间:2023-02-13 08:00:57

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

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

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.

现在,我希望在结果数据的第一行中包含列的名称。否则,在出口时,它们不包括在内,我没有找到允许后者的任何选项。

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

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

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.

我知道如果列具有类型的值,比如Integer会有一些问题,但是我可能会简单地将所有内容(如何?)转换为字符串。

1 个解决方案

#1


3  

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.

这将是一个快速而肮脏的方法来做你想要的。如果列是非varchar2类型,那么您需要将第二个SELECT结果转换为VARCHAR2。

See TO_CHAR for the conversion syntax.

有关转换语法,请参阅TO_CHAR。

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.

如果您真的不想看到“排序”列,那么您可以将整个语句包装在另一个选择中,该选择只选择您想要查看的列。

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.

我不得不同意这些意见,理想情况下,您应该能够通过IDE工具导出元数据,而不是解决方法。

Hope it helps...

希望能帮助到你...

#1


3  

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.

这将是一个快速而肮脏的方法来做你想要的。如果列是非varchar2类型,那么您需要将第二个SELECT结果转换为VARCHAR2。

See TO_CHAR for the conversion syntax.

有关转换语法,请参阅TO_CHAR。

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.

如果您真的不想看到“排序”列,那么您可以将整个语句包装在另一个选择中,该选择只选择您想要查看的列。

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.

我不得不同意这些意见,理想情况下,您应该能够通过IDE工具导出元数据,而不是解决方法。

Hope it helps...

希望能帮助到你...