如何在oracle中添加select输出头的双引号?

时间:2014-10-13 05:09:22

标签: sql oracle

如何在oracle的select输出中的每个标题上加上双引号? 因为我需要在csv文件中导出它,我希望它像这样

例如

"columnaHeader1", "columnHeader2", "columnHeader3"
"col1data1","col2data1","col3data1"
"col2data2","col2data2","col3data2"

因为在我当前的查询中我喜欢

e.g

case when blahblah is not null then '""' end  as "header" from tablename

标题仍未用双引号括起来..如何在oracle中执行此操作?

2 个答案:

答案 0 :(得分:2)

只需连接双引号,应该很简单:

SQL> SELECT '"'||ename||'"' as "columnheader1",
  2         '"'||job||'"' as "columnheader2"
  3  FROM emp
  4  /

columnheader columnheade
------------ -----------
"SMITH"      "CLERK"
"ALLEN"      "SALESMAN"
"WARD"       "SALESMAN"
"JONES"      "MANAGER"
"MARTIN"     "SALESMAN"
"BLAKE"      "MANAGER"
"CLARK"      "MANAGER"
"SCOTT"      "ANALYST"
"KING"       "PRESIDENT"
"TURNER"     "SALESMAN"
"ADAMS"      "CLERK"
"JAMES"      "CLERK"
"FORD"       "ANALYST"
"MILLER"     "CLERK"

14 rows selected.

SQL>

要摆脱上述查询中的标题,请将PAGESIZE设置为零。要添加逗号分隔符,请设置COLSEP

SQL> set pagesize 0
SQL> set colsep ,
SQL> SELECT '"'||ename||'"' as "columnheader1",
  2         '"'||job||'"' as "columnheader2"
  3  FROM emp
  4  /
"SMITH"     ,"CLERK"
"ALLEN"     ,"SALESMAN"
"WARD"      ,"SALESMAN"
"JONES"     ,"MANAGER"
"MARTIN"    ,"SALESMAN"
"BLAKE"     ,"MANAGER"
"CLARK"     ,"MANAGER"
"SCOTT"     ,"ANALYST"
"KING"      ,"PRESIDENT"
"TURNER"    ,"SALESMAN"
"ADAMS"     ,"CLERK"
"JAMES"     ,"CLERK"
"FORD"      ,"ANALYST"
"MILLER"    ,"CLERK"

14 rows selected.

答案 1 :(得分:1)

参考Schema Object Names and Qualifiers

  

带引号的标识符可以包含任何字符和标点符号以及空格。但是,引号和非引号标识符都不能包含双引号或空字符(\ 0)

所以你必须将标题导出为数据。

你可以像这样在DUAL表中联合标题:

select 'columnaHeader1', 'columnHeader2', 'columnHeader3' from DUAL
union all
select ...