【问题标题】:Check if a string value matches up with the content of an existing table in Oracle 11G检查字符串值是否与 Oracle 11G 中现有表的内容匹配
【发布时间】:2015-05-19 15:36:09
【问题描述】:

目前我的工作效率并不高。对于我遇到的问题,我几乎可以肯定有一种更智能、更好的方法来解决它。

我正在尝试做的事情: 我得到一个这样的字符串:

'NL 4633 4809 KTU'

NL 是现有表格中的国家/地区代码,KTU 是现有表格中的大学代码。我需要将此字符串放入我的函数中并检查该字符串是否经过验证。

在我的函数(验证字符串)中,这是我正在处理的。我已经设法用这个分割字符串:

 countryCode := checkISIN; -- checkISIN is the full string ('NL 4633 4809 KTU') and I am giving the NL value to this variable. countryCode is the type varchar2(50)
 countryCode := regexp_substr(countryCode, '[^ ]+', 1, 1);

现在我有了如下所示的国家代码:

NL
Has valid country code

我想从它自己的表中验证/检查国家代码是否存在。我试过这个:

if countryCode in ('NL', 'FR', 'DE', 'GB', 'BE', 'US', 'CA')
 then dbms_output.put_line('Has valid country code');
 else
 dbms_output.put_line('Has invald country code. Change the country code to a valid one');
 end if;

这可行,但不是动态的。如果有人添加了国家代码,那么我必须再次更改该功能。

那么有没有一种(智能/动态)方法来检查他们现有表格的国家代码?

希望我的问题不要太含糊

干杯

【问题讨论】:

标签: regex oracle if-statement oracle11g


【解决方案1】:

如果你有国家代码表,它看起来像这样:

ID | NAME
----------
1  | NL
2  | FR
3  | BE

当你解析字符串时,你可以这样:

 select count(1)
        into v_quan
        from CountryCodes cc
       where nvl(countryCode,'') = cc.name
if v_quan > 0 then
  dbms_output.put_line('Has valid country code');
else 
  dbms_output.put_line('Has invald country code. Change the country code to  a valid one');  
end if;   

【讨论】:

  • 这样就行了。所以如果我理解正确的话。 count(1) 检查 ID 号?如果数字大于 0(确实存在 1、2、3 等),那么它是否有效?
  • count(1) 仅作为示例,如果结果返回任何值 (0,1,2,3,..) - 都将有效,如果这让您感到困惑 - 您可以只使用count(*) 或 count(id) 等。
  • 啊。我明白你在说什么。无论如何,我要感谢您提供的遮阳篷,并将您的遮阳篷标记为合适的。干杯
  • 很高兴为您提供帮助!谢谢
猜你喜欢
  • 1970-01-01
  • 2010-12-08
  • 1970-01-01
  • 2017-03-10
  • 2010-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多