SQL 带大小写的语句
SQL statement with case
我想要一个接收薪水值和 job_id 的函数。此函数需要验证作为参数传递的工资值是否介于最高工资和最低工资之间。
CREATE FUNCTION check(revenue Number, id integer)
RETURN message
IS message varchar2;
BEGIN
select min(revenue), max(revenue) from users;
RETURN(message);
END;
除非 message
是一种数据类型(很可能不是),否则您不能 return 它。你想要的 return 是一个字符串 - varcahr2
数据类型。
所以:
SQL> create or replace function f_test (salary in number)
2 return varchar2
3 is
4 l_min number;
5 l_max number;
6 begin
7 select min(sal), max(sal)
8 into l_min, l_max
9 from emp;
10
11 return case when salary between l_min and l_max then 'Between'
12 when salary > l_max then 'Above max'
13 else 'Other'
14 end;
15 end;
16 /
Function created.
SQL> select f_test(6000) result from dual;
RESULT
-------------------------------------------------------------------------------
Above max
SQL>
随时改进它(通过添加职位、部门等)。
您可以通过使用解析函数创建一个不返回极值局部变量的函数作为选项:
CREATE OR REPLACE FUNCTION verifyIfSalaryIsBetweenMinAndMaxForThatJob(i_salary NUMBER,
i_jobId INT)
RETURN VARCHAR2 IS
message VARCHAR2(50);
BEGIN
SELECT MAX(CASE WHEN i_salary < min_sal THEN
'Below'
WHEN i_salary > max_sal THEN
'Above Max'
WHEN i_salary BETWEEN min_sal AND max_sal THEN
'Between'
END)
INTO message
FROM
(
SELECT MIN(salary) OVER (PARTITION BY job_Id) AS min_sal,
MAX(salary) OVER (PARTITION BY job_Id) AS max_sal,
e.*
FROM employees e
)
WHERE job_id = i_jobId;
RETURN message;
END;
/
我想要一个接收薪水值和 job_id 的函数。此函数需要验证作为参数传递的工资值是否介于最高工资和最低工资之间。
CREATE FUNCTION check(revenue Number, id integer)
RETURN message
IS message varchar2;
BEGIN
select min(revenue), max(revenue) from users;
RETURN(message);
END;
除非 message
是一种数据类型(很可能不是),否则您不能 return 它。你想要的 return 是一个字符串 - varcahr2
数据类型。
所以:
SQL> create or replace function f_test (salary in number)
2 return varchar2
3 is
4 l_min number;
5 l_max number;
6 begin
7 select min(sal), max(sal)
8 into l_min, l_max
9 from emp;
10
11 return case when salary between l_min and l_max then 'Between'
12 when salary > l_max then 'Above max'
13 else 'Other'
14 end;
15 end;
16 /
Function created.
SQL> select f_test(6000) result from dual;
RESULT
-------------------------------------------------------------------------------
Above max
SQL>
随时改进它(通过添加职位、部门等)。
您可以通过使用解析函数创建一个不返回极值局部变量的函数作为选项:
CREATE OR REPLACE FUNCTION verifyIfSalaryIsBetweenMinAndMaxForThatJob(i_salary NUMBER,
i_jobId INT)
RETURN VARCHAR2 IS
message VARCHAR2(50);
BEGIN
SELECT MAX(CASE WHEN i_salary < min_sal THEN
'Below'
WHEN i_salary > max_sal THEN
'Above Max'
WHEN i_salary BETWEEN min_sal AND max_sal THEN
'Between'
END)
INTO message
FROM
(
SELECT MIN(salary) OVER (PARTITION BY job_Id) AS min_sal,
MAX(salary) OVER (PARTITION BY job_Id) AS max_sal,
e.*
FROM employees e
)
WHERE job_id = i_jobId;
RETURN message;
END;
/