我这里有一个从示例数据库调用表列的存储过程,但我的问题是我想将一个列名称变成一个变量,以便我可以在条件中使用它。我的代码不起作用,但我不知道如何将其作为变量。
我想将列名SALARY作为变量。
这是我的代码:
CREATE PROCEDURE TAXEXEMPT ()
DYNAMIC RESULT SETS 1
LANGUAGE SQL
BEGIN
DECLARE display CURSOR WITH RETURN TO CLIENT FOR
SELECT EMPNO, LASTNAME, SALARY, SALARY AS "TAX EXEMPTION"
FROM EMPLOYEE
WHERE WORKDEPT BETWEEN 'C01' AND 'D21';
IF ( SALARY < 15001 ) THEN
--DO NOTHING
ELSEIF ( SALARY >= 15001 OR SALARY <= 22000 ) THEN
UPDATE EMPLOYEE SET "TAX EXEMPTION" = SALARY * 1.03;
ELSEIF ( SALARY >= 22001 OR SALARY <= 32000 ) THEN
UPDATE EMPLOYEE SET "TAX EXEMPTION" = (SALARY * 1.05) + 100;
ELSEIF ( SALARY >= 32001 OR SALARY <= 45000 ) THEN
UPDATE EMPLOYEE SET "TAX EXEMPTION" = (SALARY * 1.07) + 200;
ELSE
--DO NOTHING
END IF;
OPEN display;
END
提前感谢任何形式的帮助。
答案 0 :(得分:3)
你想要的是一个CASE表达式:
DECLARE display CURSOR WITH RETURN TO CLIENT FOR
SELECT EMPNO, LASTNAME, SALARY,
CASE
WHEN SALARY < 15001 THEN SALARY
WHEN SALARY BETWEEN 15001 AND <= 22000 THEN SALARY * 1.03
WHEN SALARY BETWEEN 20001 AND <= 32000 THEN (SALARY * 1.05) + 100
...
ELSE SALARY
END AS "TAX EXEMPTION"
FROM EMPLOYEE
WHERE WORKDEPT BETWEEN 'C01' AND 'D21';
OPEN display;