如何在plsql块中执行delete语句并在过程中调用它

时间:2019-11-21 10:48:11

标签: oracle stored-procedures plsql

我已经在pl sql块下面编写了代码,并试图创建一个过程。但是我收到警告,无法执行该程序。

请建议我是否缺少\

请问这个问题是否重复,因为我无法获得确切的链接以供参考

DatabaseHandler db = new DatabaseHandler();
Foo foo = db.get<Foo>(1);
// and probably later
List<Foo> foo = db.getList<Foo>();

3 个答案:

答案 0 :(得分:0)

不确定是否键入了错误的内容,或者是否与设置方式有关。但是即使它是错误的类型并且可以正常工作,也不是一件好事,所以请执行循环。

i = 0;
FOR r in (select * FROM EmployeeMstrTbl where JoiningDate between to_date('2019-01-01','YYYY-MM-DD') and to_date('2019-02-28','YYYY-MM-DD'))
  LOOP
  DELETE FROM Employeetable where EmplId = r.EmployeeId;
END LOOP;
DBMS_OUTPUT.PUT_LINE('Deleted '|| i ||' records from Employeetable'); 

因为这将起作用,更重要的是,它更易于理解。由于代码的大小几乎从来都不是问题,如今,使代码简短和缩写已变得不那么重要,但是保持易于理解非常重要,这样才能在将来进行维护。

答案 1 :(得分:0)

尝试使用光标

CREATE OR REPLACE PROCEDURE EMPLOYEEPROC IS 
CURSOR C1 IS 
SELECT EMPLOYEEID 
FROM EMPLOYEEMSTRTBL 
WHERE JOININGDATE BETWEEN TO_DATE('2019-01-01','YYYY-MM-DD') AND TO_DATE('2019-02-28','YYYY-MM-DD'));
BEGIN
    FOR I IN C1 LOOP        
        DELETE FROM EMPLOYEETABLE 
        WHERE EMPLID=I.EMPLOYEEID;
    END LOOP;

    COMMIT;

    DBMS_OUTPUT.PUT_LINE('DELETED '||SQL%ROWCOUNT ||' RECORDS FROM EMPLOYEETABLE');

END;

答案 2 :(得分:0)

您的代码可以正常工作。

CREATE TABLE Employeetable
(
    EmplId NUMBER
);

CREATE TABLE EmployeeMstrTbl
(
    EmployeeId NUMBER,
    JoiningDate DATE
);

CREATE OR REPLACE PROCEDURE EmployeeProc
IS
BEGIN
    DELETE FROM Employeetable
          WHERE EmplId IN
                    (SELECT EmployeeId
                       FROM EmployeeMstrTbl
                      WHERE JoiningDate BETWEEN TO_DATE ('2019-01-01',
                                                         'YYYY-MM-DD')
                                            AND TO_DATE ('2019-02-28',
                                                         'YYYY-MM-DD'));

    COMMIT;
    DBMS_OUTPUT.PUT_LINE (
        'Deleted ' || SQL%ROWCOUNT || ' records from Employeetable');
END;

EXEC EmployeeProc;

DROP TABLE Employeetable;
DROP TABLE EmployeeMstrTbl;
DROP PROCEDURE EmployeeProc;

脚本输出:

Table created.
Table created.
Procedure created.
 PL/SQL procedure successfully completed.
Table dropped.
Table dropped.
Procedure dropped.

DBMS输出:

Deleted 0 records from Employeetable

也许您在表名,列名或类似名称中有错字。

我建议您先尝试执行delete语句以检查其是否有效。