用于查找特定模式的Oracle SQL REGEXP

b4wnujal  于 2022-11-03  发布在  Oracle
关注(0)|答案(3)|浏览(202)

我正在SQL中构建一个小型项目,以自动化由于ORA-12899 -值对于列太大而发生的加载失败。
从一个错误存储库表中,我可以得到如下所示的错误消息,
“ORA-12899:列“SCOTT”.“TABLE_EMPLOYEE”.“NAME”的值太大(实际值为15,最大值为10)
我希望使用正则表达式从上述错误消息中提取以下DDL,

ALTER TABLE TABLE_EMPLOYEE MODIFY NAME VARCHAR2(15);

下面是我目前的代码。有了这个,我只能提取模式名称,即“SCOTT”
SELECT REGEXP_SUBSTR('ORA-12899: value too large for column "SCOTT"."TABLE_EMPLOYEE"."NAME" (actual:15 , maximum: 10)','"([^"]+)"',1,1,NULL,1) AS RESULT from DUAL;
如果有人能用正确的正则表达式或任何其他方法来帮助我提取这些信息,我将不胜感激

igetnqfo

igetnqfo1#

您可以使用正则表达式:

SELECT REGEXP_REPLACE(
         error,
         '^.*(".*?"\.".*?")\.(".*?")\s+\(\s+actual:\s+(\d+).*$',
         'ALTER TABLE \1 MODIFY \2 VARCHAR2(\3)'
       ) AS query
FROM   table_name

其中,对于示例数据:

CREATE TABLE table_name (error) AS
SELECT 'ORA-12899:value too large for column "SCOTT"."TABLE_EMPLOYEE"."NAME" ( actual: 15, maximum: 10 )' FROM DUAL UNION ALL
-- Handle quoted identifiers with numbers
SELECT 'ORA-12899:value too large for column "test1"."actual 20"."VALUE20" ( actual: 25, maximum: 7 )' FROM DUAL;

输出:
| 查询|
| - -|
| 修改“名称”VARCHAR 2(15)|
| 更改表格“测试1”。“实际值20”修改“值20”变量字符2(25)|
fiddle

bwntbbo3

bwntbbo32#

您需要编写如下PL/SQL块:

cl scr
set SERVEROUTPUT ON

declare
err varchar2(3000);
cmnd varchar2(3000);
scma varchar2(100);
tbl varchar2(500);
clm varchar2(200);
N varchar2(100);

begin

err:='ORA-12899:value too large for column "SCOTT"."TABLE_EMPLOYEE"."NAME" ( actual 15, maximum:10 )';

select regexp_substr(err, '\w+', 1, 8) into scma from dual; -- Extract 8th word
select regexp_substr(err, '\w+', 1, 9) into tbl from dual;  -- Extract 9th word
select regexp_substr(err, '\w+', 1, 10) into clm from dual;  -- Extract 10th word
select regexp_substr(err, '\w+', 1, 12) into N from dual; -- Extract 12th word

cmnd:='ALTER TABLE "'||scma||'"."'||tbl||'" modify "'||clm||'" varchar2('||N||');';
dbms_output.put_line(cmnd);

end;

结果是:

ALTER TABLE "SCOTT"."TABLE_EMPLOYEE" modify "NAME" varchar2(15);

实际上,您应该生成一个动态SQL,就像您看到的那样。通过这个,您可以从错误中提取的信息生成任何命令。

6fe3ivhb

6fe3ivhb3#

这里有一个选项,它只使用substr + instr组合。
截至您的示例数据:这很可能是伪造的。“真实的”错误消息如下所示:

SQL> create table table_employee (name varchar2(10));

Table created.

SQL> insert into table_employee (name) values ('Littlefoot Wiki');
insert into table_employee (name) values ('Littlefoot Wiki')
                                          *
ERROR at line 1:
ORA-12899: value too large for column "SCOTT"."TABLE_EMPLOYEE"."NAME" (actual: 15, maximum: 10)

SQL>

在处理这样的字符串时,你只需要精确,否则你会得到意想不到的(错误的)结果。

You   : ORA-12899:value too large for column "SCOTT"."TABLE_EMPLOYEE"."NAME" ( actual 15, maximum:10 )
Really: ORA-12899: value too large for column "SCOTT"."TABLE_EMPLOYEE"."NAME" (actual: 15, maximum: 10)

还有一个例子(对于@MT0 's-正确-反对我之前的代码),表和列包含数字:

SQL> create table test15 (col12umn varchar2(2));

Table created.

SQL> insert into test15 values ('abc');
insert into test15 values ('abc')
                           *
ERROR at line 1:
ORA-12899: value too large for column "SCOTT"."TEST15"."COL12UMN" (actual: 3, maximum: 2)

SQL>

因此:

SQL> with test (col) as
  2    (select 'ORA-12899: value too large for column "SCOTT"."TEST15"."COL12UMN" (actual: 3, maximum: 2)'       from dual union all
  3     select 'ORA-12899: value too large for column "SCOTT"."TABLE_EMPLOYEE"."NAME" (actual: 15, maximum: 10)' from dual
  4    )
  5  select
  6    'alter table ' ||
  7    substr(col, instr(col, '"', 1, 1),
  8                instr(col, '"', 1, 4) - instr(col, '"', 1, 1) + 1
  9          ) ||
 10    ' modify ' ||
 11    substr(col, instr(col, '"', 1, 5),
 12                instr(col, '"', 1, 6) - instr(col, '"', 1, 5) + 1
 13          ) ||
 14    ' varchar2(' ||
 15    substr(col, instr(col, ': ', -1, 2) + 2,
 16                instr(col, ',', -1, 1) - instr(col, ': ', -1, 2) - 2
 17          ) || ')' as result
 18  from test;

RESULT
----------------------------------------------------------------------------------------------------
alter table "SCOTT"."TEST15" modify "COL12UMN" varchar2(3)
alter table "SCOTT"."TABLE_EMPLOYEE" modify "NAME" varchar2(15)

SQL>

相关问题