用存储过程实现高级搜索代码

 

这个实际上就是模糊查询,如果做个网站是多用户的话,模糊查询用的多些。请参考下面我的代码,修改下字段和参数就可以使用了。呵呵!!
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
########################用户表组合查询###################################

CREATE proc sp_searchUser
(
@UserName varchar(25)=null,
@UserSchool varchar(20)=null,
@UserEmail varchar(50)=null,
@UserQQ varchar(20)=null,
@UserMSN varchar(50)=null,
@UserCard varchar(20)=null,
@UserRank varchar(10)=null
)
as
declare  @sqlstr varchar(1000)
set @sqlstr='select  * from uupo_User where 1=1'
if (@UserName <>'')
   set @sqlstr=@sqlstr   + 'and User_name like "%'+@UserName+'%"'
if (@UserSchool <>'')
   set @sqlstr=@sqlstr   + 'and User_School like "%'+@UserSchool+'%"'
if (@UserEmail <>'')
   set @sqlstr=@sqlstr   + 'and User_email like "%'+@UserEmail+'%"'
if (@UserQQ <>'')
   set @sqlstr=@sqlstr   + 'and User_qq like "%'+@UserQQ+'%"'
if (@UserMSN <>'')
   set @sqlstr=@sqlstr   + 'and User_Msn like "%'+@UserMSN+'%"'
if (@UserCard <>'')
   set @sqlstr=@sqlstr   + 'and User_card like "%'+@UserCard+'%"'
if (@UserRank <>'')
   set @sqlstr=@sqlstr   + 'and User_rank like "%'+@UserRank+'%"'

exec(@sqlstr)
GO

你可能感兴趣的:(用存储过程实现高级搜索代码)