以下是一个根据不同情况条件查询相应信息的存储过程,总觉得用if else 很没意思,怎么做条件查询才好呢,这些代码又怎么优化?高手指点一下好吗?代码如下:
create proc getList @title varchar(20),@userID int,@time varchar(20),@Tsid int
 as
if(@title!='' and @time!='' and @Tsid!=0)
begin
select * from bbsTopic where Ttopic like '%'+@title+'%' and convert(varchar(20),Ttime,120) like '%'+@time+'%' and Tuid=@userID and Tsid=@Tsid
end
if(@title!='' and @time!='' and @Tsid=0)
begin
select * from bbsTopic where Ttopic like '%'+@title+'%' and convert(varchar(20),Ttime,120) like '%'+@time+'%' and Tuid=@userID
end
if(@title!='' and @Tsid!=0 and @time='')
begin
select * from bbsTopic where Ttopic like '%'+@title+'%' and Tsid=@Tsid and Tuid=@userID
end
if(@title!='' and @Tsid=0 and @time='')
begin
select * from bbsTopic where Ttopic like '%'+@title+'%' and Tuid=@userID
end
if(@title='' and @Tsid!=0 and @time!='')
begin
select * from bbsTopic where convert(varchar(20),Ttime,120) like '%'+@time+'%' and Tsid=@Tsid and Tuid=@userID
end
if(@title='' and @Tsid=0 and @time!='')
begin
select * from bbsTopic where convert(varchar(20),Ttime,120) like '%'+@time+'%' and Tuid=@userID
end
if(@title='' and @Tsid!=0 and @time='')
begin
select * from bbsTopic where Tsid=@Tsid and Tuid=@userID
end
if(@title='' and @time='' and @Tsid=0 and @userID!=0)
begin
select * from bbsTopic where Tuid=@userID
end
GO

解决方案 »

  1.   

    如果条件是互斥的,最好做成if elseif 的形式.其实,if-else是个很好的东西,如果做条件查询,其实用这个,比用动态语句要好得多.在固定的数据库中,我喜欢用这种方式,而不用动态.它的效率要比动态高很多.
      

  2.   

    我想知道,其实那些婚恋网站啊,购物网站类似于淘宝这样的,或者一些信息网站啊,搜索网站啊,他们的是、搜索条件那么多那么复杂,到底是怎么写查询语句的呢,难道真的一句一句if else写下来吗,那样岂不是要非常麻烦的判断,我好像觉得这些网站到底是怎么写搜索的,很强大啊,有谁知道吗?
      

  3.   

    if --
    第2個開始 else if 才對 吧
      

  4.   

    怎么样写成一个结果集呢?
    这里写else if和写if是一样的
      

  5.   

        你应该用动态拼接,嵌套一下判断拼接出SQL语句执行就Ok了
      

  6.   

    --try
    create proc getList 
       @title varchar(20)=null,
       @userID int=null,
       @time datetime=null,
       @Tsid int=null
     as
     declare @sql nvarchar(4000)
     set @sql=N'select * from bbsTopic where 1=1 '
              +case when @title is not null then 
              N' and Ttopic like %@title%' else N'' end
              +case when @userID is not null then
              N'Tuid=@userID 'else N'' end
              +case when @time is not null then
              N'datediff(ss,Ttime,@time)=0' else N'' end
              +case when @Tsid is not null then
              N'Tsid=@Tsid' else N'' end
    exec sp_executesql
         @sql,
         N'@title as varchar(20),@userID as int,@time as datetime,@Tsid as int ',
         @title=@title,
         @userID =@userID ,
         @time=@time,
         @Tsid=@Tsid
      

  7.   

    我的执行语句是这样的:exec getList2 'java',1,'2010-11-02',1 
    报错:
    消息 102,级别 15,状态 1,第 1 行
    '@title' 附近有语法错误。
      

  8.   

    我有几个and忘记写了
    你写上没 and 前面加点空格
      

  9.   

    create proc getList2 
       @title varchar(20)=null,
       @userID int=null,
       @time datetime=null,
       @Tsid int=null
     as
     declare @sql nvarchar(4000)
     set @sql=N'select * from bbsTopic where 1=1 '
              +case when @title is not null then 
              N' and Ttopic like %@title%' else N'' end
              +case when @time is not null then
              N' and datediff(ss,Ttime,@time)=0' else N'' end
              +case when @Tsid is not null then
              N' and Tsid=@Tsid' else N'' end
    exec sp_executesql
         @sql,
         N'@title as varchar(20),@userID as int,@time as datetime,@Tsid as int ',
         @title=@title,
         @userID =@userID ,
         @time=@time,
         @Tsid=@Tsid
    GO
    我改成这样啦,也加了and加空格啦,还是报那个错:
    消息 102,级别 15,状态 1,第 1 行
    '@title' 附近有语法错误。
      

  10.   

    @title varchar(20)改成 nvarchar(40)
    准备下班了
    你再看看吧
      

  11.   

    create proc getList2  
      @title nvarchar(40)=null,
      @userID int=null,
      @time datetime=null,
      @Tsid int=null
     as
     declare @sql nvarchar(4000)
     set @sql=N'select * from bbsTopic where 1=1 '
      +case when @title is not null then  
      N' and Ttopic like ''%''+@title+''%''' else N'' end
      +case when @time is not null then
      N' and datediff(ss,Ttime,@time)=0' else N'' end
      +case when @Tsid is not null then
      N' and Tsid=@Tsid' else N'' end
    exec sp_executesql
      @sql,
      N'@title as nvarchar(40),@userID as int,@time as datetime,@Tsid as int ',
      @title=@title,
      @userID =@userID ,
      @time=@time,
      @Tsid=@Tsid
    GO
      

  12.   

    这样写是没报错了,用原来的exec getList 'java',1,'2010-11-02',1
    显示出是三条数据没错,但是一执行:exec getList2 'java',1,'2010-11-02',1
    这个执行明明应该有三条数据的,但却变成了没有数据,汗
      

  13.   

    本帖最后由 roy_88 于 2011-09-28 17:54:13 编辑
      

  14.   


    create proc getList2  
      @title nvarchar(40)=null,
      @userID int=null,
      @time varchar(20)=null,
      @Tsid int=null
     as
     declare @sql nvarchar(4000)
     set @sql=N'select * from bbsTopic where 1=1 '
      +case when @title is not null then  
      N' and Ttopic like ''%''+@title+''%''' else N'' end
      +case when @time is not null then
      N' and convert(varchar(20),Ttime,120) like ''%''+@time+''%''' else N'' end
      +case when @Tsid is not null then
      N' and Tsid=@Tsid' else N'' end
    exec sp_executesql
      @sql,
      N'@title as nvarchar(40),@userID as int,@time as varchar(20),@Tsid as int ',
      @title=@title,
      @userID =@userID ,
      @time=@time,
      @Tsid=@Tsid
    GO