REGEX使用动态SQL

25 浏览
0 Comments

REGEX使用动态SQL

我有以下正则表达式,在使用SQL时非常完美,但无法包含在动态SQL查询中,尝试了转义所有字符,但仍然报错。\n以下是我尝试过的内容:\n reg := \'select regexp_replace(data, ||\'\\.||(docx|pdf|msg)|| \', ||\'.\\1, \') from table where id=1\'\n\n你能帮助我将其包含在动态SQL中吗?\n

   select regexp_replace(data, '\.(docx|pdf|msg) ', '.\1, ') from table where id=1;

0
0 Comments

问题出现的原因是使用了动态SQL,但查询语句中并没有动态的部分,所以使用动态SQL是没有必要的。解决方法是检查是否真的需要使用动态SQL,以及确定要解决的实际问题是否需要动态SQL。

代码如下:

SQL> declare
  2    reg varchar2(500);
  3  begin
  4    reg := q'[select regexp_replace(data, ||'\.||(docx|pdf|msg)|| ', ||'.\1, ') from table where id=1]';
  5  end;
  6  /
PL/SQL procedure successfully completed.
SQL>

如果确实需要使用动态SQL,可以使用q-quoting机制来处理单引号的问题。

0