如何在SQL Server中使用此case语句?
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="row" id="first-page">
<div class = "row">
<select id="animal">
<option value = "bird">Bird</option>
<option value = "cat">Cat</option>
<option value = "Insect">Insect</option>
</select>
</div>
<button class="btn waves-effect waves-light" id="continue-button">
CONTINUE
</button>
</div>
<div class="row" id="second-page">
<div class = "row">
<select id="animal-type">
<option value = "hen">Hen</option>
<option value = "lion">Lion</option>
<option value = "butterfly">Butterfly</option>
</select>
</div>
</div>
如果这样的查询怎么办?
declare @isnot tinyint
select @isnot = 1
select top 100 *
from Employee
where EmployeeID > 1
and case
when @isnot = 1
then (EmployeeName = 'Brian')
else (EmployeeName = 'Anie')
end
我不想使用任何子查询或类似的东西。
答案 0 :(得分:3)
declare @isnot tinyint
select @isnot = 1
SELECT top 100 *
FROM Employee
WHERE
EmployeeID > 1 AND
EmployeeName =
CASE @isnot
WHEN 1
THEN 'Brian'
ELSE 'Anie'
END
答案 1 :(得分:0)
因为您正在使用值二进制检查(代码只关心值1或不是1),您还可以使用IIF来缩短语法。
DECLARE @isnot tinyint = 1
SELECT TOP 100 *
FROM Employee
WHERE EmployeeID > 1 AND
EmployeeName = IIF(@isnot = 1, 'Brian', 'Anie')
答案 2 :(得分:0)
语法是关键,你需要END来完成CASE语句,而EmployeeName也开始了AND ...所以:
EITHER
SELECT TOP 100 * FROM Employee WHERE EmployeeID > 1
AND EmployeeName = CASE WHEN @isnot = 1 THEN 'Brian' ELSE 'Anie' END
OR
SELECT TOP 100 * FROM Employee WHERE EmployeeID > 1
AND EmployeeName = CASE WHEN @isnot = 1 THEN 'Brian' ELSE '' END
答案 3 :(得分:0)
无案例陈述
WHERE EmployeeID > 1
AND (EmployeeName == 'Brian' AND @isnot = 1
OR EmployeeName == 'Anie')