如何测试字符串是否在预定义字符串列表中?
我定义了一个字符串列表,其中包含不同的国家/地区代码(例如 USA,CHINA,HK,JPN
等)。
如何检查输入变量是否是列表中的国家/地区代码?
我使用以下代码进行测试,但失败。
declare
country_list CONSTANT VARCHAR2(200) := USA,CHINA,HK,JPN;
input VARCHAR2(200);
begin
input := 'JPN';
IF input IN (country_list)
DBMS_OUTPUT.PUT_LINE('It is Inside');
else
DBMS_OUTPUT.PUT_LINE('It is not Inside');
END IF;
end;
I defined a list of strings, which contains different country codes (like USA,CHINA,HK,JPN
, etc.).
How can I check, if an input variable is the country code in the list?
I use the following code to test, but it fails.
declare
country_list CONSTANT VARCHAR2(200) := USA,CHINA,HK,JPN;
input VARCHAR2(200);
begin
input := 'JPN';
IF input IN (country_list)
DBMS_OUTPUT.PUT_LINE('It is Inside');
else
DBMS_OUTPUT.PUT_LINE('It is not Inside');
END IF;
end;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果你能保证输入的内容不包含分隔符,你可以这样做:
If you can guarantee that the input will not include the delimiter, you can do this:
如果您只想检查几个国家,您可以将列表写入 if 语句:
如果您想检查大量值中是否存在特定值,您可能需要使用表格:
另一种可能性是使用 MULTISET 指令:
对于非常大量的数据,您可能不希望对
MULTISET INTERSECT
使用DISTINCT
,因为这可能会导致响应时间很长。If you only want to check a few countries, you can write the list into the if-Statement:
If you want to check the existence of a specific value within a larger number of values, you maybe want to use a table:
Another possibility would be to use the MULTISET instruction:
For very large amounts of data, you may not want to use
DISTINCT
for theMULTISET INTERSECT
because this can lead to very long response time.或者,您可以将国家/地区代码列表定义为集合,并使用运算符
MEMBER OF
进行检查:Alternatively, you may define the list of the country codes as a collection, and check with the operator
MEMBER OF
: