【发布时间】:2020-09-06 19:27:02
【问题描述】:
我有以下带有CASE 语句的代码:
--to check if any record with this conditon present in table
--then pick col1, if such record exists
CASE WHEN EXISTS (SELECT col1 from table where col2=1 and col3 is not null)
THEN (SELECT col1 from table where col2=1 and col3 is not null)
ELSE
<something else>
END
where 中的条件更多,代码看起来很大。在WHEN 案例和THEN 案例中也可以看到重复。
重写代码的更好方法。对性能没有影响
【问题讨论】:
-
"那里的条件更多,代码看起来很大。"如果您的查询比这大得多,请向我们展示整个查询。但是,这似乎您可以只使用
ISNULL:ISNULL(({Your Subquery}), {Something else}) -
我将如何使用 ISNULL 选择 col1 值..你能详细说明上述建议
-
“你将如何挑选 Col1”是什么意思?
SELECT Col1
标签: sql-server tsql case