一个界面输入的变量人名 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 ....
检索该条数据,因为输入变量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 ....
SNAME = " & tsrtName
"select * from Tab1 where tsrtName where (SNAME is null and '' = '" &
tsrtName & "') or SNAME = '" & tsrtName &"';"
"select * from Tab1 where tsrtName where (SNAME is null and '' = '" &
tsrtName & "') or (SNAME = '" & tsrtName &"' " &
"and '' <> " &tsrtName ");"
在
"select * from Tab1 where tsrtName where (SNAME is null and '' = '" &
tsrtName & "') or (SNAME = '" & tsrtName &"' " &
"and '' <> " &tsrtName ");"
中
'' = '" & tsrtName & "' 不是会 ''='' 可以这样吗?
如果直接写成''=''这个就没有意思了,因为肯定成立嘛
即判断是否满足tsrtName=''的条件
"select * from Tab1 where (SNAME is null and '' = '" &
tsrtName & "') or (SNAME = '" & tsrtName &"' " &
"and '' <> " &tsrtName ");"
"select * from Tab1 where (SNAME is null and '' = '" &
tsrtName & "') or (SNAME = '" & tsrtName &"' " &
"and '' <> '" &tsrtName "');"
----------
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
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)"
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