【问题标题】:How to check multiple conditions by passing parameters to a SQL Server stored procedure如何通过将参数传递给 SQL Server 存储过程来检查多个条件
【发布时间】:2014-11-26 05:05:03
【问题描述】:

我在程序中遇到了问题。

我必须在过程中使用 if else 检查多个条件,这需要更多时间来执行。

程序结构如下

create procedure sp_CheckMultipleconditions
    @Con1 varchar(10) = null,
    @con2 int = null,
    @con3 int = null,
    ..
    @Con6 varchar(10) = null
as
BEGIN
   if(@con1 is not null)
   begin
      ----- statement ----
   end

   else if(@Con2 is not null)
   begin
      ----- statement ----
   end
   .
   .
   .
   else if(@Con2 is not null and @Con3 is not null)
   begin
     .
     .
     .
   end
END

通过这种方式,我编写了具有 36 个 if else 条件的程序,这需要更多时间,有人可以提出更好的方法吗?

谢谢

【问题讨论】:

    标签: sql-server if-statement stored-procedures parameters


    【解决方案1】:

    试试这个:

    如果第二个变量为空,那么它将返回true

    select * from table
    where (mycol1=@Con1 or @Con1 is null)
    and (mycol2=@Con2 or @Con2 is null)
    and (mycol3=@Con3 or @Con3 is null)
    and (mycol4=@Con4 or @Con4 is null)
    and (mycol5=@Con5 or @Con5 is null)
    

    【讨论】:

      猜你喜欢
      • 2021-04-28
      • 2013-06-07
      • 1970-01-01
      • 1970-01-01
      • 2017-12-29
      • 1970-01-01
      • 2015-08-20
      • 2015-12-20
      • 2013-07-06
      相关资源
      最近更新 更多