简体   繁体   中英

Control structures (IF/ELSE)

I try to create plsql block but it doesnt work. Oracle says that i have error at line 2. I think it could be because substitution variable is not placed right !?

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;

Since there can be many employees with the last name passed by the user, you may use an implicit CURSOR FOR LOOP . Also, add first_name in the output for clarity.

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;
/

Execution

..
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.

不知道这是否是您的问题,但是当您已经确保where子句中的last_name等于v_ename时,实际上无需在v_ename中选择last_name。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM