简体   繁体   English

PL / SQL和SQL%ROWCOUNT错误

[英]Error in PL/SQL and SQL%ROWCOUNT

I am trying to create a PL/SQL Procedure. 我正在尝试创建一个PL / SQL过程。 But it's not working properly. 但是它不能正常工作。 Please have look at the code and advise on this. 请查看代码并就此提出建议。

CREATE OR REPLACE PROCEDURE TEST IS

DECLARE
EMPLOYEENUM EMPLOYEE.E#%type;
EMPLOYEENAME EMPLOYEE.NAME%type;
NUMRECORDS NUMBER(2);

BEGIN

SELECT EMP.E# INTO EMPLOYEENUM ,EMP.NAME INTO EMPLOYEENAME
FROM EMPLOYEE EMP
WHERE EXISTS
(SELECT * FROM MECHANIC, DRIVER
WHERE EMP.E# = DRIVER.E# 
AND EMP.E# = MECHANIC.E#);

SELECT COUNT(*) INTO NUMRECORDS
FROM (
SELECT EMP.E#,EMP.NAME
FROM EMPLOYEE EMP
WHERE EXISTS
(SELECT * FROM MECHANIC, DRIVER
 WHERE EMP.E# = DRIVER.E# 
 AND EMP.E# = MECHANIC.E#));

IF (NUMRECORDS > 0) THEN
    DBMS_OUTPUT.PUT_LINE('ERROR: CANNOT MAKE MORE THEN 2 TRIPS PER DAY');
    ROLLBACK;
ELSE
    COMMIT;
END IF;
EXCEPTION
WHEN OTHERS THEN
    DBMS_OUTPUT.PUT_LINE(SQLERRM);
    ROLLBACK;
END VERIFY;

And I am sure I don't need to run the code again to see the number of row counts. 而且我确定我不需要再次运行代码来查看行数。 I understand there is something called 我了解有一种叫做

SQL%ROWCOUNT SQL%ROWCOUNT

which returns the row count. 返回行数。 Please advise how to use it. 请告知如何使用。

You can use an IDE and compile to see where it is failing. 您可以使用IDE并进行编译,以查看发生故障的位置。 If you are trying to compile this in SQLPLUS and you just see "Compiled with errors" message, Type SHOW ERRORS and you'll see the errors. 如果您尝试在SQLPLUS中进行编译,而只是看到“ Compiled with errors”消息,请键入SHOW ERRORS,然后您将看到错误。 I would still recommend an IDE like sql developer though. 我仍然会推荐像SQL Developer这样的IDE。

Here are some issues that I can see.... 这是我可以看到的一些问题...。

  1. Declare keyword : Not necessary when you are creating a procedure or function 声明关键字:创建过程或函数时不需要
  2. Your create has procedure name as "TEST" and your end has "VERIFY" 您创建的过程名称为“ TEST”,结尾为“ VERIFY”
  3. SELECT EMP.E# INTO EMPLOYEENUM ,EMP.NAME INTO EMPLOYEENAME : you should list all columns first and then the variable names. SELECT EMP.E# INTO EMPLOYEENUM ,EMP.NAME INTO EMPLOYEENAME :您应该首先列出所有列,然后列出变量名。 It should be SELECT EMP.E#,EMP.NAME INTO EMPLOYEENUM, EMPLOYEENAME 它应该是SELECT EMP.E#,EMP.NAME INTO EMPLOYEENUM, EMPLOYEENAME
  4. Not sure what you are committing, since there is no DML here, but if your plan is to call this procedure from a trigger, you might hit the mutating table error if the insert is into Mechanic or Driver. 由于此处没有DML,因此不确定您要提交的内容,但是如果您打算从触发器调用此过程,那么如果将插入内容插入Mechanic或Driver中,则可能会遇到变异表错误。

More background and more error information would be useful. 更多背景信息和更多错误信息将很有用。

There are some issues... 有一些问题...

  1. DECLARE keyword is not necessary to use in the procedure or function. 在过程或函数中不必使用DECLARE关键字。
  2. Procedure name started as "TEST" and end is "VERIFY" 程序名称以“ TEST”开头,以“ VERIFY”结尾
  3. You used "SELECT EMP.E# INTO EMPLOYEENUM ,EMP.NAME INTO EMPLOYEENAME", you should use all columns first and then the variable names. 您使用了“ SELECT EMP.E#INTO EMPLOYEENUM,EMP.NAME INTO EMPLOYEENAME”,应首先使用所有列,然后使用变量名。 It should be SELECT EMP.E#,EMP.NAME INTO EMPLOYEENUM, EMPLOYEENAME 它应该是SELECT EMP.E#,EMP.NAME INTO EMPLOYEENUM,EMPLOYEENAME
  4. You did not use any DML in beetween the procedure and you are using ROLLBACK, Its no of use. 您在过程之间没有使用任何DML,并且正在使用ROLLBACK,它没有用。

If you can provide the table structure of " Mechanic " and " Driver ", then I can send you the code which may help you. 如果您可以提供“ 机械师 ”和“ 驱动程序 ”的表结构,那么我可以向您发送可以帮助您的代码。

Thanks. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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