SQL问题,帮一忙!谢谢了

来源:百度知道 编辑:UC知道 时间:2024/06/05 07:48:36
我有一个语句要查询好几个字段,但是有可能有的字段为空,我这样写成吗?
<%if request("title")="" then
title=%
else
title=request("title")
enf if
if request("lx")="" then
lx=%
else
lx=request("lx")
end if
set rs=server.createobject("adodb.recordset")
sql="select * form biao where title='"&title&"'" and lx='"&lx&"'%>

一般地,像这种情况,都是采用动态生成sql查询字符串来解决.
此例中可以如下写代码:
<%
title=request("title")
lx=request("lx")
sql="select * from biao where "

if title<>"" then
sql=sql&"title='"&title&"' and "
end if

if lx<>"" then
sql=sql&"lx='"&lx&"'"
else
if sql="select * from biao where " then
response.write("没有任何查询条件")
sql=left(sql,len(sql)-7)
else
sql=left(sql,len(sql)-5)
end if
end if

set rs=server.createobject("adodb.recordset")
rs.open sql,conn,1,1
%>

我有一个语句要查询好几个字段,但是有可能有的字段为空,我这样写成吗?
<%if request("title")="" then
title=%
else
title=request("title")
enf if
if request("lx")="" then
lx=%
else
lx=request("lx")
end if