如何将多个 if case then else 情况写成一个 case
How to write multiple if case then else situation as a single case
好的标题我很难表达所以可以编辑
我的问题是我可以用更短的方式写下面的查询吗
在所有情况下都是一样的
SQL 服务器 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;
这不行吗?
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
好的标题我很难表达所以可以编辑
我的问题是我可以用更短的方式写下面的查询吗
在所有情况下都是一样的
SQL 服务器 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;
这不行吗?
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