Statement not getting printing in pl/sql

Viewed 331

A line is not getting printed

DECLARE 
R integer(10);
N varchar(20);
B VArchar(20);
Y VARCHAR(10);
A Integer(10);
E varchar2(10);
    
    CURSOR s IS 
    SELECT RollNo,Name, Branch, Year, Attendance_Percentage, Event FROM N_ROLLCALL
    WHERE branch='Comp' ORDER BY ROLLNO;
BEGIN
    OPEN s;
        LOOP
            FETCH s INTO R,N,B,Y,A,E ;
                 EXIT WHEN s%notfound;

            BEGIN
                DBMS_OUTPUT.PUT_LINE('      ROLLNO:'||R||'      NAME:'||N||'        BRANCH:'||B||'      YEAR:'||Y||'        ATTANDANCE_PERCENTAGE:'||A||'       EVENTS:'||E);
            END;
         
        END LOOP;
    CLOSE s;
    EXCEPTION
         WHEN DUP_VAL_ON_INDEX THEN
            dbms_output.put_line('You have tried to insert a duplicate roll_no.');
         WHEN OTHERS THEN
            dbms_output.put_line ('An error has occurred inserting a roll_no.');
END;

Output that I am getting

1 row(s) inserted.

0.01 seconds

But the line that I want to print using dbms_output.put_line is not getting printed when I have rows more than one which have branch ='comp'. If there is only one such entry then it is getting printed.

1 Answers

DBMS_OUTPUT is not printed when using APEX because it is not running from a console. To get a success message to appear, you will want to set the global variable APEX_APPLICATION.g_print_success_message to whatever you want your success message to be. If you want to raise an error, you will want to use the APEX_ERROR package. If there are any errors added using the APEX_ERROR package, any transactions will not be committed when APEX has finished its processing.

DECLARE
    R   INTEGER (10);
    N   VARCHAR (20);
    B   VARCHAR (20);
    Y   VARCHAR (10);
    A   INTEGER (10);
    E   VARCHAR2 (10);

    CURSOR s IS
          SELECT RollNo,
                 Name,
                 Branch,
                 Year,
                 Attendance_Percentage,
                 Event
            FROM N_ROLLCALL
           WHERE branch = 'Comp'
        ORDER BY ROLLNO;
BEGIN
    OPEN s;

    LOOP
        FETCH s
            INTO R,
                 N,
                 B,
                 Y,
                 A,
                 E;

        EXIT WHEN s%NOTFOUND;

        BEGIN
            APEX_APPLICATION.g_print_success_message (
                   '      ROLLNO:'
                || R
                || '      NAME:'
                || N
                || '      BRANCH:'
                || B
                || '      YEAR:'
                || Y
                || '      ATTANDANCE_PERCENTAGE:'
                || A
                || '      EVENTS:'
                || E);
        END;
    END LOOP;

    CLOSE s;
EXCEPTION
    WHEN DUP_VAL_ON_INDEX
    THEN
        apex_error.add_error (p_message            => 'You have tried to insert a duplicate roll_no.',
                              p_display_location   => apex_error.c_inline_in_notification);
    WHEN OTHERS
    THEN
        apex_error.add_error (p_message            => 'An error has occurred inserting a roll_no.',
                              p_display_location   => apex_error.c_inline_in_notification);
END;
Related