Search code examples
sqlstored-proceduresprocedurestored-functions

Printing header for PL SQL Procedure


I have the following SQL Procedure code:

create or replace procedure numOfSupplier(X IN NUMBER)IS
OUTPUT VARCHAR(500);

BEGIN 

    FOR supplerTable IN (SELECT R_NAME, N_NAME
    FROM SUPPLIER join NATION on S_NATIONKEY = N_NATIONKEY
    join REGION on N_REGIONKEY = R_REGIONKEY
    GROUP BY R_NAME, N_NAME
    HAVING COUNT (S_NATIONKEY) > X)

LOOP

DBMS_OUTPUT.PUT_LINE (supplerTable.R_NAME || supplerTable.N_NAME);

END LOOP;

END numOfSupplier;
/

When ran, gives me: https://i.imgur.com/zEEKpya.png

I'm trying to have this show: https://i.sstatic.net/OLXPF.png

Any idea how to print the header out?

DBMS_OUTPUT.PUT_LINE seems limited

Thanks in advance!

PS, I tried having a DBMS_OUTPUT.PUT_LINE before the loop but it errored out:

12/1 PLS-00103: Encountered the symbol "DBMS_OUTPUT" when expecting one of the following: loop The symbol "loop" was substituted for "DBMS_OUTPUT" to continue.

20/5 PLS-00103: Encountered the symbol "NUMOFSUPPLIER" when expecting one of the following: loop

Error when trying:

OUTPUT VARCHAR(500);

BEGIN 

    FOR supplerTable IN (SELECT R_NAME, N_NAME
    FROM SUPPLIER join NATION on S_NATIONKEY = N_NATIONKEY
    join REGION on N_REGIONKEY = R_REGIONKEY
    GROUP BY R_NAME, N_NAME
    HAVING COUNT (S_NATIONKEY) > X)

DBMS_OUTPUT.PUT_LINE ('HELLO');

LOOP

DBMS_OUTPUT.PUT_LINE (supplerTable.R_NAME || supplerTable.N_NAME);

END LOOP;

END numOfSupplier;
/

Solution

  • You cannot put the DBMS_OUTPUT.PUT_LINE in the middle of the loop's header. You have to put it completely before the loop.

    ...
    DBMS_OUTPUT.PUT_LINE ('<The header goes here>');
    
    FOR supplerTable IN (...) LOOP
      DBMS_OUTPUT.PUT_LINE (supplerTable.R_NAME || supplerTable.N_NAME);
    END LOOP;
    ...