【问题标题】:Use a variable with "LIKE %" (e.g. "variable%") in PL/SQL?在 PL/SQL 中使用带有“LIKE %”的变量(例如“variable%”)?
【发布时间】:2012-04-26 09:38:15
【问题描述】:

问题类似于在 SQL *PLUS 中使用 LIKE,其中 select 语句包含 LIKE 子句,如下所示:

select * from sometable where somecolumn LIKE 'something%';

如何在光标中使用相同的内容?我尝试使用以下内容:

 cursor c is select * from sometable where somecolumn like 'something%'; 

同上

编辑:我需要将 something 作为参数,这意味着 select 语句在存储过程中执行。

编辑 2:

create procedure proc1 (search VARCHAR) is

cursor c is select student_name from students where student_name like 'search%';

--我知道使用“search%”检索包含“key search”的学生姓名,但有没有其他方法可以使用这样的变量。

do something;

end;

简而言之,我需要选择包含作为参数传递的值的学生姓名;这可能不是全名,但足以在 like 子句中使用。

【问题讨论】:

  • 所以……你试过了……发生了什么
  • 您确定的原因吗? CURSOR x IS {select} 应该是有效的,其中{select} 代表任意选择 DQL。
  • 我得到一个变量,something作为参数,并在光标内使用它。
  • 请发布一个完整(但最少)的上下文,说明其他人可以尝试的错误。我相信这个简化中缺少一个重要的部分。 'something%' 只是文字;不涉及任何变量。
  • 帖子和报告的错误消息和意图不一致。我已经更新了标题给它一些方向。在您能够使用“正常”SELECT 完成之前,无需担心 CURSOR。

标签: sql oracle plsql sqlplus


【解决方案1】:

根据我对您的问题的理解,您在引号内使用变量 search。将您的变量放在引号之外,例如:

 create or replace procedure PROC1(search VARCHAR2) 
 IS
  cursor test_cur(search IN VARCHAR2)
   IS
    SELECT student_name 
    FROM student
    WHERE student_name LIKE search||'%'; --you're putting you variable within quotes

 v_temp_var student.student_name%TYPE;

BEGIN

 OPEN test_cur(search);
  LOOP
   FETCH test_cur INTO v_temp_var;
    EXIT WHEN test_cur%NOTFOUND;

    DBMS_OUTPUT.PUT_LINE(v_temp_var);  
  END LOOP;

 CLOSE test_cur;

END test;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-23
    • 1970-01-01
    • 1970-01-01
    • 2021-04-16
    • 2019-06-04
    • 2016-04-24
    相关资源
    最近更新 更多