【问题标题】:SQL contains issue in stored proceduresSQL 包含存储过程中的问题
【发布时间】:2020-04-17 04:38:21
【问题描述】:

在我正在制作的数据库上尝试包含动词,我想知道这个语法的问题

create or replace PROCEDURE past_event_check
as
BEGIN
SELECT  event_description from past_events
where Contains (event_description, 'Club');

END;

它说编译失败,第 5 行 (04:29:41) PL/SQL: ORA-00920: 无效的关系运算符编译失败,第 4 行 (04:29:41)

【问题讨论】:

    标签: sql oracle oracle-apex


    【解决方案1】:

    你应该像下面这样使用

        create or replace PROCEDURE past_event_check
        as
         l_event_desc past_events.event_description%TYPE;
         BEGIN
         SELECT  event_description into l_event_desc from past_events
        where Contains (event_description, 'Club',1) > 0;
         dbms_output.put_line(l_event_desc);
        END;
    

    【讨论】:

      【解决方案2】:

      CONTAINS 是 Oracle 文本。看这个例子:

      SQL> create table test as select * From emp;
      
      Table created.
      
      SQL> select deptno, empno, ename, job, sal from test
        2  where contains(ename, 'king') > 0;
      select deptno, empno, ename, job, sal from test
      *
      ERROR at line 1:
      ORA-20000: Oracle Text error:
      DRG-10599: column is not indexed
      
      
      SQL>
      

      对; Oracle Text - 列必须被索引,但不是任何索引类型:

      SQL> create index i1 on test(ename) indextype is ctxsys.context;
      
      Index created.
      
      SQL> select deptno, empno, ename, job, sal from test
        2  where contains(ename, 'king') > 0;
      
          DEPTNO      EMPNO ENAME      JOB              SAL
      ---------- ---------- ---------- --------- ----------
              10       7839 KING       PRESIDENT       5000
      
      SQL>
      

      好的,现在满足先决条件。

      如果您想将这样的查询移动到 PL/SQL(即存储过程)中,则需要您将数据放入某个东西 - 一个变量。声明它并在select声明中使用:

      SQL> create or replace procedure p_test (par_ename in test.ename%type) is
        2    l_job test.job%type;          --> declared is here
        3  begin
        4    select job
        5      into l_job                  --> used is here
        6      from test
        7      where contains(ename, par_ename) > 0;
        8    dbms_output.put_line(par_ename || ' works as ' || l_job);
        9  end;
       10  /
      
      Procedure created.
      
      SQL> set serveroutput on
      SQL> begin
        2    p_test('king');
        3  end;
        4  /
      king works as PRESIDENT
      
      PL/SQL procedure successfully completed.
      
      SQL>
      

      根据您的实际操作,这可能需要修复,因为存在各种错误的危险(例如查询返回的TOO_MANY_ROWS 等)。


      如果您实际上不想使用 Oracle Text,请查看其他选项:instrlike

      SQL> select deptno, empno, ename, job, sal from test
        2  where instr(ename, 'KING') > 0;
      
          DEPTNO      EMPNO ENAME      JOB              SAL
      ---------- ---------- ---------- --------- ----------
              10       7839 KING       PRESIDENT       5000
      
      SQL> select deptno, empno, ename, job, sal from test
        2  where ename like '%KING%';
      
          DEPTNO      EMPNO ENAME      JOB              SAL
      ---------- ---------- ---------- --------- ----------
              10       7839 KING       PRESIDENT       5000
      
      SQL>
      

      【讨论】:

      • 非常感谢!这真的帮助我更多地理解它
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-07
      • 2012-04-18
      • 1970-01-01
      • 2013-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多