于项目中碰到由数据库管理员开的视图用于报表制作,但是视图在插询中出现了数据库字段的字段无效报错,报错如下
,经排查,该视图可能是由某些软件生成,而不是管理员使用sql收到生成的,用sqldevelop打开该视图的结构,查看其sql,得到以下代码
CREATE OR REPLACE FORCE VIEW "MRDCTEST"."V_METRO01" ("employee_workno", "old_workno", "employeename", "companyId", "company_name", "departmentId", "depertment_name", "status") AS
select t.employee_workno as "employee_workno",
t.old_workno as "old_workno",
t.employeename as "employeename",
t.company_id as "companyId",
t.company_name as "company_name",
t.depertment_id as "departmentId",
t.depertment_name as "depertment_name",
( case when t.curr_type in('退休','解聘','辞退','离退员工','解聘员工','开除','在职死亡','辞职','离休','身故')
then 'InActive'
else 'Active'
end) as "status"
from pf.t_user t;
由上可以看出,所有的字段都是由双引号""包裹,所以查询时候我们的字段同样需要添加"",即如下
。同时在我们的java代码中,如果需要查询该语句,面对双引号。我们需要使用转义符号\来进行编译,即如下形式:
sql1="select * from V_METRO01 where \"status\" ='Active'";
至此,关于视图查询的异常告一段落。