怎样根据传入的变量设置where 语句后的查询条件
例如:表customer 和saleInfo
customer 字段:id,name...
saleInfo 字段:id,F_CustomerID...
存储过程参数:customerID
如果customerID>0,where 后添加条件customer.id=saleInfo.F_customerID

解决方案 »

  1.   

    if @customerID >0
      @strsql=select * from table where .....And customer.id=saleInfo.F_customerID 
    else
    @strsql=select * from table where .....????
      

  2.   

    ......
    If (customerID>0 )
    @sqlstr=@sqlstr+ 'where customer.id=saleInfo.F_customerID'.......
      

  3.   

    if ...
       拼接个SQL
    else
       拼接另外一个SQLexex(你拼接的SQL)
      

  4.   

    select * from customer where 
    @customerID <= 0 or [id] in( select F_CustomerID from saleInfo  ) 
      

  5.   


    select xx from xx where customer.id = (case when customer.id > 0 then saleInfo.F_customerID else when customer.id end)
      

  6.   

    晕,多打了个whenselect xx from xx 
    where customer.id = (case when customer.id > 0 then saleInfo.F_customerID 
        else customer.id end)