你能用oraclesql中的函数作为dml吗?

rekjcdws  于 2021-07-29  发布在  Java
关注(0)|答案(2)|浏览(198)

所以我得到了一个任务来执行下面的任务。

Create a simple program to demonstrate the use of stored functions for
       a) Retrieving formatted information by giving the employee id.
       b) Performing DML commands (Insert, Update and Delete)
       c) Checking palindrome String

使用下表

SQL> desc Employee01;
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 EMPNO                                     NOT NULL NUMBER(38)
 ENAME                                     NOT NULL VARCHAR2(20)
 DEPTNO                                    NOT NULL VARCHAR2(5)
 JOB                                                VARCHAR2(20)
 HIREDDATE                                          DATE
 SALARY                                             NUMBER(38)

我已经做了前两个,但回文的一个我不知道如何开始。我们将不胜感激。
编辑:在dml回文代码中获得帮助

CREATE OR REPLACE FUNCTION FPal(
  str VARCHAR2)
     RETURN VARCHAR2
  IS
     l VARCHAR2;
     t VARCHAR2;
     res  VARCHAR2 (130);
  BEGIN
     FOR i IN REVERSE 1..Length(s) LOOP  
        l := Substr(str, i, 1);
        t := t  
             ||'' 
             ||l;  
    END LOOP;  

    IF t = str THEN 
      dbms_output.Put_line(t  
                           ||'' 
                           ||' is palindrome');  
    ELSE 
      dbms_output.Put_line(t  
                           ||'' 
                           ||' is not palindrome');  
    END IF;  
     RETURN (res);
  END FPal;
 /
nc1teljy

nc1teljy1#

下面是一个可能的固定函数:

SQL> set serveroutput on
    SQL> CREATE OR REPLACE FUNCTION FPal(
      2    str VARCHAR2)
      3       RETURN  VARCHAR2
      4    IS
      5       l CHAR;
      6       t VARCHAR2(128);
      7    BEGIN
      8       FOR i IN REVERSE 1..Length(str) LOOP
      9          l := Substr(str, i, 1);
     10          t := t  || l;
     11      END LOOP;
     12  
     13      IF t = str THEN
     14        dbms_output.Put_line(t  ||' is palindrome');
     15        return 'TRUE';
     16      ELSE
     17        dbms_output.Put_line(t  ||' is not palindrome');
     18        return 'FALSE';
     19      END IF;
     20    END FPal;
     21   /

    Function created.

    SQL> show errors
    No errors.

测试:

SQL> select fpal('ABBA') from dual;

    FPAL('ABBA')
    --------------------------------------------------------------------------------
    TRUE

    ABBA is palindrome
    SQL> select fpal('BABA') from dual;

    FPAL('BABA')
    --------------------------------------------------------------------------------
    FALSE

    ABAB is not palindrome
SQL>
6uxekuva

6uxekuva2#

另一种方法:将输入字符串拆分成行,然后向后合并。比较这两个值并决定。

SQL> create or replace function f_pal (par_val in varchar2) return varchar2 is
  2    l_val varchar2(200) := replace(par_val, ' ', '');
  3    l_rev varchar2(200);
  4  begin
  5    select listagg(regexp_substr(l_val, '[^.]', 1, level), '')
  6           within group (order by level desc)
  7      into l_rev
  8      from dual
  9      connect by level <= length(l_val);
 10
 11    return 'palindrome: ' || case when l_rev = l_val then 'YES'
 12                                  else 'NO'
 13                             end;
 14  end;
 15  /

Function created.

测试:

SQL> with
  2  test (col) as
  3    (select 'little' from dual union all
  4     select 'radar'  from dual union all
  5     select '12321'  from dual union all
  6     select 'ana voli milovana' from dual
  7    )
  8  select col, f_pal(col) result
  9  from test;

COL               RESULT
----------------- ------------------------------
little            palindrome: NO
radar             palindrome: YES
12321             palindrome: YES
ana voli milovana palindrome: YES

SQL>

相关问题