0

我对存储过程和使用游标为多行运行 SELECT 有点困惑。我需要在 SQL 查询中使用 LIKE 子句。我已经测试过这实际上与我的存储过程一起使用:

SQL> /* THIS WORKS*/
SQL> create or replace procedure search_testimonials(
  2  curRETURN OUT sys_refcursor
  3  )
  4  IS
  5  begin
  6   OPEN curRETURN FOR 
  7   SELECT testimonial
  8   FROM testimonial
  9   WHERE testimonial like '%like%';
 10  END search_testimonials;
 11  /

Procedure created.

SQL> variable buffer refcursor;
SQL> execute Search_Testimonials(:buffer);

PL/SQL procedure successfully completed.

SQL> print buffer;

TESTIMONIAL
--------------------------------------------------------------------------------
I like the way OAG does business. I will be a repeat customer
I like the Cashier named David. I think he is a hottie! Go AOGS

当我尝试将参数用作搜索词的一部分时,就会出现问题:

SQL> /* THIS DOES NOT WORK*/
SQL> create or replace procedure search_testimonials(
  2  search_term IN varchar2,
  3  curRETURN OUT sys_refcursor
  4  )
  5  IS
  6  begin
  7   OPEN curRETURN FOR 
  8   SELECT testimonial
  9   FROM testimonial
 10   WHERE testimonial like search_term||'%%';
 11  END search_testimonials;
 12  /

Procedure created.

SQL> 
SQL> execute Search_Testimonials('like', :buffer);

PL/SQL procedure successfully completed.

SQL> print buffer;

no rows selected

我不知道为什么这不起作用。我还尝试使用@VARIABLE 表示法来传递正确的术语。我正在使用 SQLPlus 运行代码。感谢您的任何建议。

4

1 回答 1

2

改变

 WHERE testimonial like search_term||'%%'

 WHERE testimonial like '%'|| search_term||'%'
于 2013-03-24T18:46:04.897 回答