strsql="city="&chenshiidif sjtyep="0" or isnull(sjtyep) or trim(sjtyep)="" then
else
strsql=strsql&" and en_hanye='"&sjtyep&"'"
end if if sjname<>"" then
strsql=strsql&" and en_name like '%"& sjname &"%'"
end if
if huanwei<>"" then
strsql=strsql&" and en_huanwei like '%"& huanwei &"%'"
end if
我只知道这种查询效率比较低,听说写成子查询会效率会高很多,请高手指教
else
strsql=strsql&" and en_hanye='"&sjtyep&"'"
end if if sjname<>"" then
strsql=strsql&" and en_name like '%"& sjname &"%'"
end if
if huanwei<>"" then
strsql=strsql&" and en_huanwei like '%"& huanwei &"%'"
end if
我只知道这种查询效率比较低,听说写成子查询会效率会高很多,请高手指教
declare @sql varchar(max)
set @sql='select * from tablename where 1=1'declare @id int
if(@id<>'' and @id is not null)
set @sql=@sql+' and id='+ltrim(@id)
---------------------等价于
select * from tablename where id=isnull(id,@id) and @id<>''