2014-10-16 131 views
0

我在比較if語句中的字符串時遇到問題。這是我有:爲什麼這是if語句失敗?

declare @old varchar(max), @manual varchar(max) 
set @old = (select ruledef from roolz where ruleid = 1234) 
set @manual = 'String responseDate = subject.getField("Response Due Date");' + char(10) 
    + 'if (responseDate != null && responseDate .trim().length() > 0) {' + char(10) 
    + ' Map params = new HashMap();' + char(10) 
    + ' params.put("Response Due Date",responseDate);' + char(10) + char(10) + 
    + 'ruleUtil.launchActivity(subject,"PCT-RESP",params,"Launch_PCTRESP",false);' + char(10) 
    + char(10) + '}' + char(10) 
print @manual 
print @old 
--if (@old like '%' + @manual + '%') 
if (@old = @manual) 
begin 
    print 1; 
end 

當我運行這個它打印出以下表明@old@manual是相同的:

String responseDate = subject.getField("Response Due Date"); 

if (responseDate != null && responseDate .trim().length() > 0) { 
    Map params = new HashMap(); 
    params.put("Response Due Date",responseDate); 

    ruleUtil.launchActivity(subject,"PCT-RESP",params,"Launch_PCTRESP",false); 
} 

String responseDate = subject.getField("Response Due Date"); 

if (responseDate != null && responseDate .trim().length() > 0) { 
    Map params = new HashMap(); 
    params.put("Response Due Date",responseDate); 

    ruleUtil.launchActivity(subject,"PCT-RESP",params,"Launch_PCTRESP",false); 
} 

但不打印1,如果語句失敗意味。我也試過

if (@old like '%' + @manual + '%') 

,它仍然無法打印1.

誰能告訴我,爲什麼這個if語句失敗的原因?

+0

爲什麼選擇投票? – 2014-10-16 19:26:14

+2

所需要的只是一個字符不同 - 它可能是一個非打印字符('LF' vs'CR/LF'?)或一個字符,它們看起來像是兩個字符串中的空格,但不是。我會循環逐字,直到找到不同。 – 2014-10-16 19:27:29

+0

這裏寫的字符串確實是一樣的;我檢查了。當它們被粘貼到堆棧溢出時,可能會丟失一些東西。也許嘗試換行符和回車符的不同組合('char(10)'和'char(13)')? – 2014-10-16 19:28:36

回答

0

我最終找到了解決方案。我正在考慮換行符,但我沒有考慮製表符。我逃脫了換行符和製表符,以及它應該如此工作。感謝@D Stanley指導我思考其他角色的方向。

0

我不能告訴你爲什麼測試失敗了,但不是看它和被難倒了,你可以改變你的IF測試挖掘出爲什麼一個你已經有了心不是工作:

if (@old = @manual) print 1;  --this one obviously won't happen 
if (substring(@old, 1, 100) = substring(@manual, 1, 100)) print 2; 
if (len(@old) = len(@manual) print 3; 
if (ltrim(rtrim(@old)) = ltrim(rtrim(@manual))) print 4; 
etc