正确使用pl / sql中的游标?

时间:2015-04-21 15:03:13

标签: sql oracle plsql

我正在尝试这样输出:

In the case of a valid employee or employees:
Enter value for sv_firstname: chris
Enter value for sv_lastname: miller
Employee ID: 41822 Description: Cashier Pay Rate: $11.63
Employee ID: 80809 Description: Butcher Pay Rate: $19.26

PL/SQL procedure successfully completed.

这是我到目前为止所得到的:

DECLARE
  CURSOR emp_cursor IS
  Select e.empid, j.description, j.payrate from 
  employee e, job j where j.jobcode = e.jobcode AND 
  lower(lastname) = lower(lname) and 
  lower(firstname) = lower(fname);
   fname employee.FIRSTNAME%TYPE:='&fname';
   lname employee.LASTNAME%TYPE:='&lname';
   emp employee.EMPID%TYPE;
   descript job.DESCRIPTION%TYPE;
   p_rate job.payrate%TYPE;

BEGIN 
  OPEN emp_cursor;

LOOP
  FETCH emp_cursor INTO emp,descript,p_rate;
  DBMS_OUTPUT.PUT_LINE(fname||' '||lname);
  DBMS_OUTPUT.PUT_LINE('Employee Number: ' || emp);
  DBMS_OUTPUT.PUT_LINE('Description: ' || descript);
  DBMS_OUTPUT.PUT_LINE('Pay Rate: ' || p_rate);      
  EXIT WHEN  emp_cursor%NOTFOUND;
  END LOOP;
  Close emp_cursor;      
END; 
/

这是我得到的错误

错误报告 -

ORA-06550: line 5, column 11:
PL/SQL: ORA-00920: invalid relational operator
ORA-06550: line 3, column 4:
PL/SQL: SQL Statement ignored
ORA-06550: line 10, column 15:
PLS-00103: Encountered the symbol "," when expecting one of the following:

   := . ( @ % ; not null range default character
06550. 00000 -  "line %s, column %s:\n%s"
*Cause:    Usually a PL/SQL compilation error.
*Action:

2 个答案:

答案 0 :(得分:1)

我想通了

DECLARE
    fname employee.FIRSTNAME%TYPE:='&fname';
    lname employee.LASTNAME%TYPE:='&lname';   
    emp employee.EMPID%TYPE;
    descript job.DESCRIPTION%TYPE;
    p_rate job.payrate%TYPE;   
  CURSOR emp_cursor IS
   Select e.empid, j.description, j.payrate from 
    employee e, job j where j.jobcode = e.jobcode AND
    lower(lastname) = lower(lname) and 
    lower(firstname) = lower(fname);

BEGIN 
  OPEN emp_cursor;

LOOP
  FETCH emp_cursor INTO emp,descript,p_rate;
  EXIT WHEN  emp_cursor%NOTFOUND;
  DBMS_OUTPUT.PUT_LINE('Employee Number: ' || emp);
  DBMS_OUTPUT.PUT_LINE('Description: ' || descript);
  DBMS_OUTPUT.PUT_LINE('Pay Rate: ' || p_rate); 

  END LOOP;
   if emp_cursor%ROWCOUNT<1 THEN
    DBMS_OUTPUT.PUT_LINE(fname||' '||lname||' does not exist');
  END IF;
  Close emp_cursor; 

END; 
/

答案 1 :(得分:1)

目前您的主要问题是您在变量之前有光标声明;它需要反过来,而你在错误的地方退出。

这至少会运行:

DECLARE
  fname employee.FIRSTNAME%TYPE:='&fname';
  lname employee.LASTNAME%TYPE:='&lname';
  emp employee.EMPID%TYPE;
  descript job.DESCRIPTION%TYPE;
  p_rate job.payrate%TYPE;

  CURSOR emp_cursor IS
    Select e.empid, j.description, j.payrate from 
    employee e, job j where j.jobcode = e.jobcode AND 
    lower(lastname) = lower(lname) and 
    lower(firstname) = lower(fname);

BEGIN 
  OPEN emp_cursor;

  LOOP
    FETCH emp_cursor INTO emp,descript,p_rate;
    EXIT WHEN  emp_cursor%NOTFOUND;
    DBMS_OUTPUT.PUT_LINE(fname||' '||lname);
    DBMS_OUTPUT.PUT_LINE('Employee Number: ' || emp);
    DBMS_OUTPUT.PUT_LINE('Description: ' || descript);
    DBMS_OUTPUT.PUT_LINE('Pay Rate: ' || p_rate);      
  END LOOP;
  Close emp_cursor;      
END; 
/

它不会以您想要的格式生成输出,但您现在应该可以使用它。您还应该考虑使用ANSI连接语法而不是Oracle旧的逗号分隔语法。在这里使用绑定变量而不是替换变量可能更简单。