Oracle SQL - 查询结果与查询变量为整数与字符串时不一致



我有一个名为"svc_id_value"的数据库字段,其中存储了字母数字值和整数值。

我有一个查询来搜索与此服务 ID 相关的详细信息。 当我的输入变量是整数时,查询工作正常,但是当我尝试使用字母数字字符串进行相同的查询时,结果返回为空。 有问题的字段 (svc_id_value) VARCHAR2(30 字节)。 在"数据"视图中,我可以在 Oracle SQL 开发人员中成功筛选字母数字变量,但是当我在 SQL 开发人员中查询它时,结果返回为空。

工作查询(字符串为整数 - 搜索 s.svc_id_value='555555'):

SELECT * 
from svc s, status_types stat, service_types st,
billing_types bt, account ax, building pp, locations ll, 
ref_countries cx 
WHERE 
s.svc_status = stat.status_type 
and stat.tablename='service' 
and s.svc_type = st.service_type 
and s.svc_billing_type = bt.billing_type 
and ax.account_id = s.svc_account_id 
and pp.bldg_id = s.svc_bldg_id 
and ll.location_id = s.svc_location_id 
and cx.id = ll.country_id 
and s.svc_id_value ='555555';

非工作查询(字符串为字母数字 - 搜索 s.svc_id_value='88888888-ABC'):

SELECT * 
from svc s, status_types stat, service_types st,
billing_types bt, account ax, building pp, locations ll, 
ref_countries cx 
WHERE 
s.svc_status = stat.status_type 
and stat.tablename='service' 
and s.svc_type = st.service_type 
and s.svc_billing_type = bt.billing_type 
and ax.account_id = s.svc_account_id 
and pp.bldg_id = s.svc_bldg_id 
and ll.location_id = s.svc_location_id 
and cx.id = ll.country_id 
and s.svc_id_value ='88888888-ABC';

为了确认按字母数字变量进行查询是否有效,以下简单查询成功返回结果:

SELECT * from svc s WHERE s.svc_id_value='88888888-ABC'

我不明白为什么查询会使用整数,但在字母数字时失败。

有什么想法吗?

为了解决此类问题,我会将所有内部联接更改为外联接,如下所示:

SELECT * 
from svc s
LEFT JOIN status_types stat
ON s.svc_status = stat.status_type 
AND stat.tablename='service' 
LEFT JOIN service_types st
ON s.svc_type = st.service_type 
LEFT JOIN billing_types bt
ON s.svc_billing_type = bt.billing_type 
LEFT JOIN account ax
ON ax.account_id = s.svc_account_id 
LEFT JOIN building pp
ON pp.bldg_id = s.svc_bldg_id 
LEFT JOIN locations ll
ON ll.location_id = s.svc_location_id 
LEFT JOIN ref_countries cx 
ON cx.id = ll.country_id 
WHERE s.svc_id_value ='88888888-ABC';

这样,您应该返回一行,并且可以浏览结果。至少有一个联接表将具有 null 值,这就是您在原始内部联接查询中返回零行的原因。

最新更新