为什么这个if语句失败了?

时间:2014-10-16 19:21:32

标签: sql sql-server if-statement

我在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语句失败。我也试过

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

并且仍然无法打印1.

有谁能告诉我为什么这个if语句失败了?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

我最终找到了解决方案。我考虑了换行符,但我没有考虑制表符。我逃脱了换行符和制表符并且它正常工作。感谢@D Stanley让我指出了思考其他角色的方向。