如果我传递一个变量值null,如何避免一个条件

时间:2015-04-10 06:48:41

标签: sql sql-server

我的问题是,如果我在一个变量中传递值,它应该检查条件,如果我在变量中传递一个空值,它应该不考虑条件。请帮助我..

例如:如果我将firstdateofservice变量传递为null,则不应检查条件

DECLARE @FirstDateofService DATE;
DECLARE @LastDateofService DATE; 

SET @FirstDateofService= '04/01/14'; --mm/dd/yyyy
SET @FirstDateofService= CONVERT(varchar(10),@FirstDateofService,21)
SET @ProcessDate  = '10/31/14'; --mm/dd/yyyy 
SET @ProcessDate=CONVERT(varchar(10),@ProcessDate,21)


SELECT DISTINCT cs.* 
    FROM   claimline cl(nolock) 
           JOIN claimsummary cs(nolock) 
             ON cs.claimsummaryuid = cl.claimsummaryuid 
    WHERE  cisid IN(@Cisid,@cisid1) 
           AND cs.firstdateofservice  >=@firstdateofservice
           and cs.lastdateofservice>=@lastdateofservice 

5 个答案:

答案 0 :(得分:1)

您可以添加以下条件:

  ...
   WHERE  cisid IN(@Cisid,@cisid1) 
         AND (cs.firstdateofservice  >=@firstdateofservice
            OR @firstdateofservice IS NULL)
       and cs.lastdateofservice>=@lastdateofservice

或者像这样添加支票:

SET @FirstDateofService = ISNULL(@FirstDateofService, '19000101')

SELECT DISTINCT cs.* 
    FROM   claimline cl(nolock) 
           JOIN claimsummary cs(nolock) 
             ON cs.claimsummaryuid = cl.claimsummaryuid 
    WHERE  cisid IN(@Cisid,@cisid1) 
           AND cs.firstdateofservice  >=@firstdateofservice
           and cs.lastdateofservice>=@lastdateofservice 

答案 1 :(得分:0)

您可以在条件中添加空检查,如下所示:

WHERE  cisid IN(@Cisid,@cisid1) 
       AND (@firstdateofservice IS NULL OR cs.firstdateofservice  >=@firstdateofservice)
       and cs.lastdateofservice>=@lastdateofservice 

答案 2 :(得分:0)

只需在此处添加null条件,如

SELECT DISTINCT cs.* 
    FROM   claimline cl(nolock) 
           JOIN claimsummary cs(nolock) 
             ON cs.claimsummaryuid = cl.claimsummaryuid 
    WHERE  cisid IN(@Cisid,@cisid1) 
           AND 
 (cs.firstdateofservice  >=@firstdateofservice  or  isnull(@firstdateofservice , '') = '')
 and (cs.lastdateofservice>=@lastdateofservice or isnull(@firstdateofservice , '') = '')

答案 3 :(得分:0)

这是你期待的吗?

DECLARE @FirstDateofService DATE;
DECLARE @LastDateofService DATE; 

SET @FirstDateofService= '04/01/14'; --mm/dd/yyyy
SET @FirstDateofService= CONVERT(varchar(10),@FirstDateofService,21)
SET @ProcessDate  = '10/31/14'; --mm/dd/yyyy 
SET @ProcessDate=CONVERT(varchar(10),@ProcessDate,21)

IF @FirstDateofService IS NULL
SELECT DISTINCT cs.* 
    FROM   claimline cl(nolock) 
           JOIN claimsummary cs(nolock) 
             ON cs.claimsummaryuid = cl.claimsummaryuid 
    WHERE  cisid IN(@Cisid,@cisid1) 
           and cs.lastdateofservice>=@lastdateofservice
ELSE
SELECT DISTINCT cs.* 
    FROM   claimline cl(nolock) 
           JOIN claimsummary cs(nolock) 
             ON cs.claimsummaryuid = cl.claimsummaryuid 
    WHERE  cisid IN(@Cisid,@cisid1) 
           AND cs.firstdateofservice  >=@firstdateofservice
           and cs.lastdateofservice>=@lastdateofservice

答案 4 :(得分:-1)

您可以使用此格式更改WHERE (column = @filter OR @filter IS NULL) AND (column2 = @filter2 OR @filter2 IS NULL)

像这样。

SELECT DISTINCT cs.* 
    FROM   claimline cl(nolock) 
           JOIN claimsummary cs(nolock) 
             ON cs.claimsummaryuid = cl.claimsummaryuid 
    WHERE  (cisid IN(@Cisid,@cisid1) OR (@Cisid IS NULL AND @cisid1 IS NULL ) )
           AND (cs.firstdateofservice  >=@firstdateofservice OR @firstdateofservice IS NULL)
           and (cs.lastdateofservice>=@lastdateofservice OR @lastdateofservice IS NULL)