如果情况如何编写多个然后情况作为单个案例

时间:2015-01-12 20:35:44

标签: sql sql-server tsql case sql-server-2014

好的标题很难让我表达,所以可以编辑

我的问题是我可以用更短的方式写下面的查询

在所有情况下,时间相同

SQL server 2014

这里是查询

    update
   tblCrawlUrls 
set
   cl_LastCrawlDate = case 
      when Len(@cl_CrawlSource) > 2 then SYSUTCDATETIME() 
      else cl_LastCrawlDate  
   end ,
   cl_TotalCrawlTimes = case 
      when Len(@cl_CrawlSource) > 2 then @cl_TotalCrawlTimes 
      else cl_TotalCrawlTimes  
   end ,
   cl_Ignored_By_Containing_Word = case 
      when Len(@cl_CrawlSource) > 2 then @cl_Ignored_By_Containing_Word 
      else cl_Ignored_By_Containing_Word  
   end ,
   cl_PageProcessed = case 
      when Len(@cl_CrawlSource) > 2 then 0 
      else cl_PageProcessed 
   end ,
   cl_CertainlyNotProductPage = case 
      when Len(@cl_CrawlSource) > 2 then @cl_CertainlyNotProductPage 
      else cl_CertainlyNotProductPage 
   end ,
   cl_CrawlSource = case 
      when Len(@cl_CrawlSource) > 2 then @cl_CrawlSource 
      else cl_CrawlSource 
   end ,
   cl_FoundLinksCount = case 
      when Len(@cl_CrawlSource) > 2 then @cl_FoundLinksCount 
      else cl_FoundLinksCount 
   end ,
   cl_DuplicateUrl_ByCanonical = case 
      when Len(@cl_CrawlSource) > 2 then @cl_DuplicateUrl_ByCanonical 
      else cl_DuplicateUrl_ByCanonical 
   end                        
where
   cl_CrawlNormalizedUrl=@cl_CrawlNormalizedUrl;

1 个答案:

答案 0 :(得分:5)

这不会起作用吗?

IF len(@cl_CrawlSource) > 2
    UPDATE tblCrawlUrls 
     set
       cl_LastCrawlDate              = SYSUTCDATETIME() 
      ,cl_TotalCrawlTimes            = @cl_TotalCrawlTimes 
      ,cl_Ignored_By_Containing_Word = @cl_Ignored_By_Containing_Word 
      ,cl_PageProcessed              = 0 
      ,cl_CertainlyNotProductPage    = @cl_CertainlyNotProductPage 
      ,cl_CrawlSource                = @cl_CrawlSource 
      ,cl_FoundLinksCount            = @cl_FoundLinksCount 
      ,cl_DuplicateUrl_ByCanonical   = @cl_DuplicateUrl_ByCanonical 
     where cl_CrawlNormalizedUrl = @cl_CrawlNormalizedUrl

除非我遗漏了某些内容,否则每个case语句都有相同的子句len(@cl_CrawlSource) > 2,如果不满足该子句,则该列保持不变,整体只影响那些行where cl_CrawlNormalizedUrl = @cl_CrawlNormalizedUrl