1

我刚开始学习 PL/SQL,我不确定如何创建一个过程。逻辑似乎是正确的,但我认为第一行存在一些语法错误。这是我的代码:-

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;
/
4

7 回答 7

5

两件事 - 您不应该在过程/函数的参数列表中指定数据类型大小,并且您不需要DECLARE关键字。尝试这个:

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;
于 2013-10-27T22:58:13.423 回答
1

在没有 PL/SQL 的情况下试一试!

WITH
params AS
  (SELECT 'supercalifragilisticexpialidocious' phrase FROM dual),
WordReverse (inpt, outpt) AS
  (SELECT phrase inpt, CAST(NULL AS varchar2(4000)) outpt FROM params
   UNION ALL
   SELECT substr(inpt,2,LENGTH(inpt)-1), substr(inpt,1,1) || outpt
   FROM wordReverse
   WHERE LENGTH(inpt) > 0
  )
SELECT phrase,outpt AS reversed FROM wordReverse, params
WHERE LENGTH(outpt) = LENGTH(phrase) ;

PHRASE                             REVERSED
---------------------------------- -----------------------------------
supercalifragilisticexpialidocious suoicodilaipxecitsiligarfilacrepus

引用:http ://rdbms-insight.com/wp/?p=94

于 2016-06-03T19:40:54.340 回答
1

另一种解决方案反向字符串最小化循环计数

DECLARE
v_str     VARCHAR2(100) DEFAULT 'MYSTRING';
v_len     NUMBER;
v_left    VARCHAR2(100);
v_right   VARCHAR2(100);
BEGIN
    v_len := LENGTH(v_str)/2;
    FOR rec IN 1..v_len 
    LOOP
      v_left := substr(v_str,rec,1) || v_left;
      IF rec * 2 <= LENGTH(v_str) THEN
        v_right := v_right || substr(v_str,-rec,1);
      END IF;
    END LOOP;
    v_str := v_right || v_left;
    dbms_output.put_line(v_str);
END;
于 2018-09-20T08:45:23.463 回答
0
set serveroutput on
declare
  str1 varchar2(30);
  len number(3);
  str2 varchar2(30);
  i number(3);
begin
  str1:='&str1';
  len:=length(str1);
  for i in reverse 1..len
   loop
    str2:=str2 || substr(str1,i,1);
   end loop;
  dbms_output.put_line('Reverse string is: '||str2);
end;
/
于 2014-02-02T14:37:17.610 回答
0
    create or replace procedure ap_reverse_number(input_no VARCHAR2) as
      output_no VARCHAR2(100);

    begin

      for i in 1 .. length(input_no) loop
        output_no := output_no || '' ||
                     substr(input_no, (length(input_no) - i + 1), 1);
      end loop;

      dbms_output.put_line('Input no. is :' || input_no);
      dbms_output.put_line('Output no. is:' || output_no);

    end;

这应该正确地完成工作。

于 2015-09-05T04:45:12.563 回答
0

试试这一行语句来反转sql中的字符串

with a as (select 'brahma' k from dual)
select listagg(substr(k,length(k)-level+1,1),'') within group (order by 1)  b from a connect by level<length(k)+1
于 2021-05-25T01:38:41.370 回答
-1
 declare
 name varchar2(20):='&name';
 rname varchar2(20);
 begin
 for i in reverse 1..length(name)
 loop
 rname:=rname||substr(name,i,1);
 end loop;
 display(rname);
 end;
于 2016-02-21T19:44:08.697 回答