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

时间:2013-10-27 22:53:45

标签: sql oracle plsql syntax-error

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

7 个答案:

答案 0 :(得分:5)

两件事 - 您不应在procedure / function的参数列表中指定数据类型大小,并且不需要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;

答案 1 :(得分: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

答案 2 :(得分:1)

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

angular cli

答案 3 :(得分: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;
/

答案 4 :(得分:0)

class Account
{
    public Account(String init_id, double init_balance)
   {
       super(init_id, init_balance);
   }
   public void condition_Method()
   {
      //your condition create object only if condition satisfied or else give                  //error message

      Account object=new Account(init_id, init_balance);
   }
}

这应该可以正常完成工作。

答案 5 :(得分: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

答案 6 :(得分:-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;