Search code examples
sqloracle-databaseplsqlsyntax-error

A procedure to Reverse a String in PL/SQL


I just started learning PL/SQL and I'm not sure how to create a procedure. The logic seems about right but I think there's some syntactical mistake in the first line. Here's my code:-

CREATE OR REPLACE PROCEDURE ReverseOf(input IN varchar2(50)) IS
DECLARE 
        reverse varchar2(50);
BEGIN
        FOR i in reverse 1..length(input) LOOP
                reverse := reverse||''||substr(input, i, 1);
        END LOOP;
        dbms_output.put_line(reverse);
END;
/

Solution

  • Two things - you shouldn't specify the datatype size in procedure's/function's parameter list and you do not need the DECLARE keyword. Try this:

    CREATE OR REPLACE PROCEDURE ReverseOf(input IN varchar2) IS
            rev varchar2(50):='';
    BEGIN
            FOR i in reverse 1..length(input) LOOP
                    rev := rev||substr(input, i, 1);
            END LOOP;
            dbms_output.put_line(rev);
    END;