我个人觉得不用笨办法很难!
我一般用以下方法:
 select * from userstable where userid=(case @userid when 0 then userid else @userid end),username=(case @username when '' then username else @username end)......

解决方案 »

  1.   

    你的条件是不是就只有@UserId 一个,你不传递任何参数是不是默认@UserId=0
    如果是 可以这样写 不用动态查询语句Create procedure upUserWorksGetList
           @UserId int=0  
    as
        IF @UserId>0
        BEGIN
         SELECT …… FROM TABLENAME
         WHERE USERID=@USERID
        END ELSE
        BEGIN
          SELECT …… FROM TABLENAME
        END
      

  2.   


    exec(sqlString)
    用exec sp_exceuteSql(sqlString,@paramlist,@paravale)
    优点: sp_executesql 中,Transact-SQL 语句的实际文本在两次执行之间未改变,所以查询优化器应该能将第二次执行中的 Transact-SQL 语句与第一次执行时生成的执行计划匹配。这样,SQL Server 不必编译第二条语句。
    Transact-SQL 字符串只生成一次。
    整型参数按其本身格式指定。不需要转换为 Unicode。 
     具体看看帮助把,希望对你有帮助
      

  3.   

    it is very hard to optimize if you are using EXEC