sql="select * from table where " if name<>"" then sql=sql & "name1='"&变量&"'" end if if belong1<>"" then sql=sql & "name2='"&变量&"'" end if if belong2<>"" then sql=sql & "name3='"&变量&"'" end if if belong3<>"" then sql=sql & "name4='"&变量&"'" end if
) WITH ENCRYPTION as set nocount on declare @SQL varchar(5000)
set @SQL='select name1,name2,name3,name4,name5 from yourtable where 1=1 ' if @name<>'' set @SQL=@SQL+ ' and name1 like ''%'+@name+'%''' if @belong1<>'' set @SQL=@SQL+ ' and name2 like ''%'+@belong1+'%'''
if @belong2<>'' set @SQL=@SQL+ ' and name3 like ''%'+@belong2+'%''' if @belong3<>'' set @SQL=@SQL+ ' and name4 like ''%'+@belong3+'%''' exec(@SQL)