Home » SQL & PL/SQL » SQL & PL/SQL » reverse function in pl/sql
reverse function in pl/sql [message #47493] Tue, 24 August 2004 02:39 Go to next message
niranjan das
Messages: 14
Registered: October 2002
Junior Member
Why can not we use reverse function in pl/sql where as we can use it in SQL ?
Re: reverse function in pl/sql [message #47494 is a reply to message #47493] Tue, 24 August 2004 03:45 Go to previous messageGo to next message
Adrian Billington
Messages: 139
Registered: December 2002
Senior Member
It works in 9i, but I haven't tried on 8i. REVERSE is a PL/SQL keyword as well as a built-in SQL function. You would need to use dynamic SQL...

 9i> declare
  2     v long;
  3  begin
  4     select reverse('hello') into v from dual;
  5     for i in reverse 1 .. 3 loop
  6        dbms_output.put_line( i || ' ' || v);
  7     end loop;
  8  end;
  9  /
3 olleh
2 olleh
1 olleh

PL/SQL procedure successfully completed.


Regards
Adrian
Re: reverse function in pl/sql [message #47495 is a reply to message #47493] Tue, 24 August 2004 04:15 Go to previous messageGo to next message
Frank Naude
Messages: 4596
Registered: April 1998
Senior Member
Hi,

Good question! Probably because it is undocumented and maybe not completely implemented within Oracle. On the other hand, there is already a REVERSE keyword in PL/SQL (see below in my_reverse function). As workaround, you can do something like this:

SQL> DECLARE
  2     v_reverse VARCHAR2(4000);
  3  BEGIN
  4     SELECT REVERSE('QWERTY') INTO v_reverse FROM dual;
  5     DBMS_OUTPUT.PUT_LINE(v_reverse);
  6  END;
  7  /
YTREWQ

PL/SQL procedure successfully completed.


Another workaround is to create your own reverse function that will work in both SQL and PL/SQL:

SQL> CREATE OR REPLACE FUNCTION my_reverse (p_str VARCHAR2) RETURN VARCHAR2 AS
  2    v_str VARCHAR2(4000);
  3  BEGIN
  4    FOR i IN <B>REVERSE</b> 1..LENGTH(p_str) LOOP
  5        v_str := v_str || SUBSTR(p_str, i, 1);
  6    END LOOP;
  7    return v_str;
  8  END;
  9  /

Function created.

SQL>
SQL> SELECT my_reverse('QWERTY') FROM dual;

MY_REVERSE('QWERTY')
--------------------------------------------------------------------------------
YTREWQ

SQL> EXEC DBMS_OUTPUT.PUT_LINE( my_reverse('QWERTY') );
YTREWQ

PL/SQL procedure successfully completed.


Best regards.

Frank
Re: reverse function in pl/sql [message #47497 is a reply to message #47493] Tue, 24 August 2004 05:37 Go to previous message
shoblock
Messages: 325
Registered: April 2004
Senior Member
Until v9, pl/sql used its own (different) sql engine, so it hadn't caught up with the database. A lot of things didn't work in pl/sql even though they worked in sql - in-line views, certain syntax for updates...

Either use dynamic sql so it actually passes control outside of pl/sql, to the better sql engine, use a user-defined procedure (provided already), or best yet, upgrade.
Previous Topic: function in order by clause
Next Topic: RE:update-inner join
Goto Forum:
  


Current Time: Tue Dec 30 18:10:24 CST 2025