一个界面输入的变量人名 strName,对应数据表TAB1 的字段 SNAME(允许为null)
检索该条数据,因为输入变量strName可能为没有输入 tstrName="" 或者 其他输入值定义SQL语句字符串变量 Dim tstrQ 
tstrQ=""
tstrQ=tstrQ & "Select * From Tab1 "
If tstrName="" then
    tstrQ=tstrQ & "Where SNAME is null; "
else
    tstrQ=tstrQ & "Where Nvl(SNAME,'')='" &  tsrtName  & "'; "
End if运行 tstrQ
说明以上代码为vb语法 &表示连接字符串问题:能不能不作 If tstrName="" then 的判断 直接生成SQL语句 Selece * From Tab1 Where ....

解决方案 »

  1.   

    "select * from Tab1 where tsrtName where (SNAME is null and '' = " & tsrtName & ") or 
    SNAME = " & tsrtName
      

  2.   

    更正一下:
    "select * from Tab1 where tsrtName where (SNAME is null and '' = '" & 
    tsrtName & "') or SNAME = '" & tsrtName &"';"
      

  3.   

    还不完整,继续修正:
    "select * from Tab1 where tsrtName where (SNAME is null and '' = '" & 
    tsrtName & "') or (SNAME = '" & tsrtName &"' " &
    "and '' <> " &tsrtName ");"
      

  4.   

    to  tommysun() 

    "select * from Tab1 where tsrtName where (SNAME is null and '' = '" & 
    tsrtName & "') or (SNAME = '" & tsrtName &"' " &
    "and '' <> " &tsrtName ");"

    '' = '" & tsrtName & "' 不是会 ''='' 可以这样吗?
      

  5.   

    不行,这个用于判断参数tsrtName是不是无内容,
    如果直接写成''=''这个就没有意思了,因为肯定成立嘛
    即判断是否满足tsrtName=''的条件
    "select * from Tab1 where (SNAME is null and '' = '" & 
    tsrtName & "') or (SNAME = '" & tsrtName &"' " &
    "and '' <> " &tsrtName ");"
      

  6.   

    还漏了一对引号
    "select * from Tab1 where (SNAME is null and '' = '" & 
    tsrtName & "') or (SNAME = '" & tsrtName &"' " &
    "and '' <> '" &tsrtName "');"
      

  7.   

    "where nvl(sname,1) = nvl('" & tsrtName & "',1);"
      

  8.   

    duanzilin(寻) 的回答也有问题,如果tsrtName输入‘1’的时候,会把null的记录也取出来。
      

  9.   

    把duanzilin(寻)的答案中的‘1‘换成tsrtName不可能输入的值即可了。
      

  10.   

    tstrQ="select * from Tab1 where (SNAME is null and '' = '" & tsrtName & "') or (SNAME = '" & tsrtName &"' and '' <> '" & tsrtName & "')"
      

  11.   

    ''和null都不可以直接用“=”或“<>”比较的,如果tsrtName为'',(SNAME is null and '' = '" & tsrtName & "')永远不会成立的,'' = '' 和'' <> ''永远都为false;
      

  12.   

    oracle中''和null应该是一致的,'' <> 'dsfs'也被认为是false;SQL> select count(*) from a;  COUNT(*)
    ----------
            13SQL> select count(*) from a where '' <> '1';  COUNT(*)
    ----------
             0SQL> select count(*) from a where '' <> '';  COUNT(*)
    ----------
             0SQL> select count(*) from a where '' = '';  COUNT(*)
    ----------
             0SQL> select count(*) from a where '' = null;  COUNT(*)
    ----------
             0
      

  13.   

    不过这么写是对的:
    SQL> select count(*) from a where '' is null;  COUNT(*)
    ----------
            13所以搂主也可以这么改:
    tstrQ="select * from Tab1 where (SNAME is null and '" & tsrtName & "' is null) or (SNAME = '" & tsrtName &"' and '" & tsrtName & "' is not null)"
      

  14.   

    tstrQ=""
    tstrQ=tstrQ & "Select * From Tab1 where 1=1 "
    If tstrName="" then
        tstrQ=tstrQ & " and SNAME is null; "
    else
        tstrQ=tstrQ & " and SNAME'" &  tsrtName  & "'; "
    End if