regexp_like不接受变量模式

时间:2016-05-31 05:00:24

标签: oracle plsql plsqldeveloper

我有一张桌子

> create table employee(employeeid number(10), name varchar2(100),deptno
> 
>     number(10));

此表中有1000行。 当我试图创建一个程序,它将搜索一个值并在运行时创建一个临时表&在该表中插入所有结果。 在过程中:tableName是将由应用程序传递的临时表的名称。 搜索字段是将在其上进行搜索的列名称。 搜索值将是模式。

create or replace PROCEDURE quick_search (tableName IN varchar2,searchfield IN 
VARCHAR2,searchvalue IN varchar2) IS
v_tableName varchar2(100);
begin
select count(tname) into v_tableName from tab where lower(tname) = tableName;
if v_tableName = 1 then
EXECUTE IMMEDIATE 'DROP TABLE '||tableName||'';
case searchfield
 when 'name' then
                    execute immediate 'create table '||create_table||'  as
                          Select Distinct employeeid FROM employee  where 
regexp_like(NAME ,'||searchvalue||',''i'')';


when 'deptno' then
                   execute immediate 'create table '||create_table||'  as
                          Select Distinct employeeid FROM employee  where 
regexp_like(deptno ,'||searchvalue||',''i'')';

end case;
end if;
if v_tableName =0 then
case searchfield
 when 'name' then
                    execute immediate 'create table '||create_table||'  as
                          Select Distinct employeeid FROM employee  where 
regexp_like(NAME ,'||searchvalue||',''i'')';


when 'deptno' then
                   execute immediate 'create table '||create_table||'  as
                          Select Distinct employeeid FROM employee  where 
regexp_like(deptno ,'||searchvalue||',''i'')';

end case;
end if;
end;

当我执行此操作时:

  

exec exec quick_search ('temp1','name','barbara') ;

在我的员工表中没有名称为temp1和barbara的对象。

我收到错误 错误报告 - ORA-00904:“BARBARA”:标识符无效 ORA-06512:在“SCOTT.QUICK_SEARCH”,第53行 ORA-06512:第1行 00904. 00000 - “%s:无效标识符” *原因: *行动:

1 个答案:

答案 0 :(得分:0)

您将双引号放在不区分大小写的参数上,但忘记将它放在模式参数上。您必须按如下方式修改程序的每个立即执行:

EXECUTE IMMEDIATE 'CREATE TABLE '||create_table||' AS SELECT DISTINCT employeeid FROM employee WHERE REGEXP_LIKE(name, ''' || searchvalue || ''',''i'')';