oracle 在PL/SQL中反转字符串的过程

oymdgrw7  于 2023-03-07  发布在  Oracle
关注(0)|答案(8)|浏览(169)

我刚开始学习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;
/
f4t66c6m

f4t66c6m1#

有两件事--你不应该在过程/函数的参数列表中指定数据类型的大小,也不需要关键字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;
yhived7q

yhived7q2#

尝试不使用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

bq8i3lrv

bq8i3lrv3#

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

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;
qgzx9mmu

qgzx9mmu4#

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

wvyml7n55#

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;

这应该能正确完成任务。

ax6ht2ek

ax6ht2ek6#

尝试使用以下一行语句反转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
xoshrz7s

xoshrz7s7#

尝试使用这一行查询来反转字符串或数字。
从双模式中选择反向(“HelloWorld”);

hjzp0vay

hjzp0vay8#

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;

相关问题