delete statement not deleting records

后端 未结 1 1236
伪装坚强ぢ
伪装坚强ぢ 2021-01-29 15:08

Anyone know why its not deleting the records from the student table. When the table is empty and i first run the anonymous block it runs fine, but then when i run it again i get

相关标签:
1条回答
  • 2021-01-29 15:37

    "doesnt my function carry out the deleting?"

    It would if you called it, but alas you don't.

    This is not a SQL problem, it's a logic problem. If we don't do the washing up the dishes remain dirty. Similarly if you don't call the routine which deletes the records the records are not deleted.

    You need to call the function in the procedure. Not sure why you've made it a function, and it won't compile anyway, because it doesn't have a RETURN clause. So, let's fix that too.

    CREATE OR REPLACE FUNCTION DELETE_ALL_STUDENTS RETURN NUMBER AS
    BEGIN
        DELETE FROM STUDENTS;
        return sql%rowcount; -- how many rows were deleted
    END;
    /
    

    Now we call it:

    create or replace PROCEDURE DELETE_ALL_STUDENTS_VIASQLDEV AS
        n number;  
    BEGIN
        dbms_output.put_line('--------------------------------------------');
        dbms_output.put_line('Deleting all student rows');
        n := DELETE_ALL_STUDENTS;
        dbms_output.put_line('No of students deleted = '|| to_char(n));
    END;
    

    So, when you run your anonymous block the existing students will be deleted and replaced with the new ones.

    0 讨论(0)
提交回复
热议问题