CREATE OR REPLACE FUNCTION letter_reverse
(char_string IN VARCHAR2)
RETURN VARCHAR2 IS
v_rev_stg VARCHAR2 := 'htimS';
BEGIN
LOOP
rev_stg := SUBSTR(rev_stg, 'h',5) || SUBSTR(rev_stg, 't',4) || SUBSTR(rev_stg, 'i',3) ||SUBSTR(rev_stg, 'm',2) || SUBSTR(rev_stg, 'S',1);
END LOOP;
END;
第7行出错:PL / SQL:忽略语句
5. BEGIN
6. LOOP
7. rev_stg := SUBSTR(rev_stg, 'h',5) || SUBSTR(rev_stg, 't',4) || SUBSTR(rev_stg, 'i',3) ||SUBSTR(rev_stg, 'm',2) || SUBSTR(rev_stg, 'S',1);
8. END LOOP;
9. END;
答案 0 :(得分:0)
如果你想要一个功能,为什么要对这些值进行硬编码?一般功能如下。
CREATE OR REPLACE FUNCTION letter_reverse
(char_string IN VARCHAR2)
RETURN VARCHAR2 IS
v_rev_stg VARCHAR2(20) := NULL;
BEGIN
for i in reverse 1..length(char_string) --loop through the length of string backwards.
LOOP
v_rev_stg := v_rev_stg||SUBSTR(char_string,i,1) ; -- append elements to v_rev_stg
END LOOP;
RETURN v_rev_stg;
END;
/
select letter_reverse('Smith') reversed FROM DUAL;
REVERSED
--------
htimS
注意: Oracle确实有一个未记录的REVERSE
函数。
答案 1 :(得分:0)
您需要考虑的另一个选择:
DECLARE
l_string VARCHAR2 (100) := '?taerg LQS/LP t''nsI';
BEGIN
DBMS_OUTPUT.put_line (
UTL_RAW.cast_to_varchar2 (
UTL_RAW.reverse (UTL_RAW.cast_to_raw (l_string))));
END;