для наглядности выведем процент в виде строки CASE DepartmentID -- проверяемое значение WHEN 2 THEN '10%' -- 10% от ЗП выдать Бухгалтерам WHEN 3 THEN '15%' -- 15% от ЗП выдать ИТ-шникам ELSE '5%' -- всем остальным по 5% END NewYearBonusPercent
CASE WHEN CVSig.ord = 1 THEN Employees.last_name ELSE Null END AS Sig_1_Emp
CASE WHEN condition1 THEN result1 WHEN condition2 THEN result2 WHEN conditionN THEN resultN ELSE result END
Otherwise, I am saying return all data i.e. SupervisorApprovedBy = SupervisorApprovedBy. What do I need to change to the below? WHERE SupervisorApprovedBy = CASE WHEN @UserRole = 'Analyst' THEN NULL ELSE SupervisorApprovedBy END. I cannot use = for NULL. Tested and does not work.
Однако, в рамках данной статьи я рассмотрю именно второй вариант, так как у него не возникает проблем с ситуациями вроде CASE WHEN NULL (null в рамках БД не является конкретным значением, поэтому его нельзя использовать в switch подобном операторе).
The CASE statement is followed by at least one pair of WHEN and THEN statements—SQL's equivalent of IF/THEN in Excel. Because of this pairing, you might be tempted to call this SQL CASE WHEN , but CASE is the accepted term. Every CASE statement must end with the END statement.
Если ни одна из операций input_expression = when_expression не дает значения TRUE, Компонент SQL Server Database Engine возвращает выражение else_result_expression, если указано предложение ELSE, или значение NULL, если предложение ELSE не указано.
In this example, my CASE expression is part of a SELECT statement. It checks for three conditions and has an ELSE to cater for anything that isn’t covered in the three conditions. In this case, the animal Cow matches the third WHEN expression, and the expression provided by its THEN is returned.
WHEN condition_n THEN result_n ELSE result END case_name. The CASE statement can be written in a few ways, so let’s take a look at these parameters.
Thus the name searched case. <result> is an arbitrary SQL expression: it can be a simple expression like a constant or column name, or also a complex SQL expression containing subqueries and, of course, nested case expressions. A case expression can contain several when-then pairs.