控制结构(IF / ELSE)

时间:2019-05-04 23:39:22

标签: oracle variables if-statement sqlplus

我尝试创建plsql块,但是它不起作用。 Oracle说我在第2行有错误。我认为可能是因为替换变量放置不正确!

DECLARE
V_ENAME EMPLOYEES.LAST_NAME%TYPE := '&LNAME';
V_SAL EMPLOYEES.SALARY%TYPE;
BEGIN
SELECT LAST_NAME, SALARY
INTO V_ENAME, V_SAL
FROM employees WHERE LAST_NAME = V_ENAME;
IF V_SAL < 3000 THEN
v_sal := v_sal + 500;
DBMS_OUTPUT.PUT_LINE (v_ename || 'have increasement ');
ELSIF V_SAL > 3000 THEN
DBMS_OUTPUT.PUT_LINE (v_ename || 'do not have increasement ');
ELSE
DBMS_OUTPUT.PUT_LINE ('error');
END IF;
END;

2 个答案:

答案 0 :(得分:1)

由于可能有许多员工使用用户传递的姓氏,因此您可以使用隐式CURSOR FOR LOOP。另外,为清楚起见,在输出中添加first_name

DECLARE
     v_ename employees.last_name%TYPE := '&LNAME';
     v_sal employees.salary%TYPE;
BEGIN

for rec IN ( SELECT first_name,last_name,salary
    FROM employees WHERE last_name = v_ename
) 
 LOOP

   IF rec.salary < 3000 THEN
     v_sal := rec.salary + 500;
     dbms_output.put_line (rec.first_name||' '||rec.last_name ||  
                         ' has an increase');
   ELSIF rec.salary  > 3000  THEN 
     dbms_output.put_line (rec.first_name||' '||rec.last_name || 
                         ' does not have an increase ');

   ELSE dbms_output.put_line ('error');
  END IF;
 END LOOP;
END;
/

执行

..
old:DECLARE
     v_ename employees.last_name%TYPE := '&LNAME';
..
new:DECLARE
     v_ename employees.last_name%TYPE := 'Grant';

..


Douglas Grant has an increase
Kimberely Grant does not have an increase 


PL/SQL procedure successfully completed.

答案 1 :(得分:0)

不确定这是否是您的问题,但在已经确保where子句中的last_name等于v_ename的情况下,实际上无需在v_ename中选择last_name。