简体   繁体   English

Oracle sql 添加自定义列名作为结果集的第一行

[英]Oracle sql adding custom column name as 1st row of the resultSet

How do I get the column name as the 1st row of the result set.I tried below but getting the error as shown below如何将列名作为结果集的第一行。我在下面尝试但得到如下所示的错误

select 'First Name', 'Last Name', 'Middle Name', 'Term Number', 'BID', 'BTitle', 'SubB Name', 'LNum' from dual 
    UNION
    select
                pa.first_name as first_name,
                pa.last_name as last_name,
                pa.MIDDLE_INITIAL as middle_name,
                bi.bi_num as num,
                bi.b_id as bId,
                b.name as b_title,
                bi.sub_board_name as sub_b_name,         
                pa.L_NUMBER as lNum
            from
                M_INFO pa,
                B_INV bi,
                Blot b,
                users u,
                roles r  
            where
                bi.assigned_to  = u.bi_num(+) 
                and bi.bi_num = pa.bi_num(+) 
                and u.role_id = r.id(+)  
                and bi.b_id = b.id
                and bi.delete_dt is null  
                and bi.delete_by is null  
                and bi.ARCHIVED_DT is null  
                and bi.b_id = '40'  and bi.sub_b_name = 'TEST'; 


ORA-01790: expression must have same datatype as corresponding expression
01790. 00000 -  "expression must have same datatype as corresponding expression"
*Cause:    
*Action:
Error at Line: 1 Column: 50

I would recommend two changes to your query.我建议对您的查询进行两项更改。

  1. Change UNION to UNION ALL .更改UNIONUNION ALL This way your union will not look for duplicates between the two parts of the union.这样您的联合就不会在联合的两个部分之间寻找重复项。
  2. Since the first part of your union is all strings, the second part of your union needs to return all strings as well.由于联合的第一部分是所有字符串,联合的第二部分也需要返回所有字符串。 Any columns in the second part of your union that are numbers need to be converted to a string.联合的第二部分中的任何数字列都需要转换为字符串。 Example: change bi.bi_num to TO_CHAR(bi.bi_num)示例:将bi.bi_num更改为TO_CHAR(bi.bi_num)

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

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