我在Oracle SQL中有一个触发器。
CREATE OR REPLACE TRIGGER test
BEFORE INSERT ON SomeTable
FOR EACH ROW
DECLARE str1 VARCHAR(30);
str2 VARCHAR(30);
BEGIN
-- some code
IF ( str1 <> str 2 ) THEN
DBMS_OUTPUT.PUT_LINE( ' if ' );
ELSE
DBMS_OUTPUT.PUT_LINE( ' else ' );
END IF;
END;现在,这总是转到else语句,即使字符串肯定不相等。我尝试使用!=而不是<>来获得相同的结果。但是,反过来,如果我只使用
IF ( str1 = str2 ) THEN ... ELSE ... END If;那么,测试两个不相等的字符串(在Oracle中)的正确方法是什么?
发布于 2011-11-07 03:55:49
你能给我们展示一下实际使用的值吗?出现上述现象的原因有可能是因为其中一个值为空?
如果str1和str2有可能有空值,那么If的值应该类似于..
IF (str1 is null and str2 is null) then
<statments depending on whether you want to treat nulls as equal>
else if (
(str1 is null and str2 is not null) or
(str2 is null and str1 is not null) or
(str1 <> str2)) then
<statements when str1 and str2 are not equal>
else
<statements when str1 and str2 are equal?
end if;发布于 2013-11-06 12:56:06
这应该确定一个字符串是否存在于另一个字符串中:
IF instr(str1,str2)<>0 THEN发布于 2013-12-31 23:21:54
有时,否定相等条件会更容易。例如,如果不相等(val1,val2);
function equals(
val1 varchar2,
val2 varchar2
) return boolean is
begin
if val1 is null then
return val2 is null;
elsif val2 is null then
return false;
end if;
return val1 = val2;
end;在你的代码中,你会有:
BEGIN
-- some code
IF NOT equals(str1, str2) THEN
DBMS_OUTPUT.PUT_LINE( ' if ' );
ELSE
DBMS_OUTPUT.PUT_LINE( ' else ' );
END IF;
END;https://stackoverflow.com/questions/8029828
复制相似问题