比如在写sql存储过程时,根据:药品名,厂家查询
其中窗体中的文本框1存放药品名,文本框2存放厂家
当文本框1里有值,文本框2无值时,进行查询。该怎么写呀????
是if(药品名!="" and 厂家="" )select * from 表名吗?

解决方案 »

  1.   

    select * from 表名 
    where (药品名=文本框1的值 or  文本框1的值='')
    or (厂家=文本框2的值 or  文本框2的值='')
      

  2.   


    不知道你是什么意思,请说明存储过程是用来做什么的,表结构是什么样的?select * 
    from 表名 
    where 药品名 = 指定的药品名 and 厂家 is null
      

  3.   

    declare @a varchar(255)--传文本框1 的参数
    declare @b varchar(255)--传文本框2 的参数select
        *
    from
        tb
    where a=case when @a is null or @a='' then a else @a end 
        and b=case when @b is null or @b='' then a else @b end
      

  4.   

    方法1:
    页面拼条件代码,当文本框1里有值,文本框2无值时,就拼成“and 药品名=”+this.文本框1.text,
    然后将这个传递给SQL,SQL最好是一个存储过程,里面使用动态SQL先将select语句拼接好,where子句直接写成:where 1=1,虽然这样不是最优化写法。然后将你传递下来的拼在后面,最后exec(动态SQL)就是。
    方法2:
    有一个文本框就有一个变量,然后将这些变量都传递给SQL,SQL判断如果变量为空则不作为where的条件,不为空则添加到where子句后面。
      

  5.   

    where (@txt1<>'' and 药名=@txt1 or @txt1='') and (@txt2<>'' and 厂家=@txt2 or @txt='')
      

  6.   

    if you like me then
    i do you 
    end