Skip to content
Advertisement

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

Advertisement

Answer

I would recommend two changes to your query.

  1. Change UNION to UNION 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)
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement