运行报错:查询块具有不正确的结果列数

来源:互联网 发布:网络售后服务包括 编辑:程序博客网 时间:2024/06/11 04:31

项目用到了oracle数据库。

项目部署到tomcat,启动服务器后,  运行报错:查询块具有不正确的结果列数。于是我将select语句复制到plsql中,以下是语句:

select a.*,       get_hrmdeptname(a.departmentid) departmentname,       get_hrmsubname(a.subcompanyid) subcompanyname,       (SELECT b.entrydate from hrm_jobinfo where b.resourceid= a.id) entrydate,       get_hrmworkname(a.workgroupid) workgroupname,       get_jobtitle(a.jobtitle) jobtitlename,       get_hrmname(a.managerid) managername  from (select *          from hrm_resource        <span style="color:#CC0000;">union all</span>        select *          from hrm_resource_leave_h h         where h.id not in (select id from hrm_resource)) a where a.status != ?   and a.lastname like ? order by a.ordernum

结果还是“查询块具有不正确的结果列数”。

在这里用到了union all。“UNION 操作符用于合并两个或多个 SELECT 语句的结果集”,union all 要求前后两个表中字段数必须相同,且对应类型相同,在这里hrm_resource_leave_h 少了2个字段,故用 null, null来补全,就是说正确的sql片段是这样的:

select *        from hrm_resource      union allselect <span style="color:#FF0000;">h.*, null, null</span>        from hrm_resource_leave_h h        where h.id not in (select id from hrm_resource)
具体关于union和union all的区别和用法,请参考下面w3cschool的链接。

0 0