PL / SQL垃圾值清理

时间:2015-08-19 14:02:20

标签: sql plsql scripting oracle-sqldeveloper

一个表包含数百万条记录,其中有12个关键列包含一些垃圾值。 并非这12列中的所有记录都是垃圾。有些记录很好,有些则是垃圾。

所以我想写一个PL / SQL脚本来清理这些垃圾值而不使用更新语句12次。 我有一个代码,但它似乎并不适用于我的事业。 任何修改代码或完全更改代码的建议都是受欢迎的。我想是动态的 SQL会在这里使用,但我不知道它到底是什么,而且我的时间非常短。

SET SERVEROUTPUT ON
DECLARE
count_total number := 0;
i           number :=0;
ch          varchar2(10);
ch2         varchar2(10);

CURSOR cursor_sim_b IS 
select a1, a2, a3, a4, a5, a6, b1, b2, b3, b4, b5, b6, -- To identify
trim(translate(a1,'0123456789',' ')) a_1,             -- garbage values
trim(translate(a2,'0123456789',' ')) a_2,
trim(translate(a3,'0123456789',' ')) a_3,
trim(translate(a4,'0123456789',' ')) a_4,
trim(translate(a5,'0123456789',' ')) a_5,
trim(translate(a6,'0123456789',' ')) a_6,
trim(translate(b1,'0123456789',' ')) b_1,
trim(translate(b2,'0123456789',' ')) b_2,
trim(translate(b3,'0123456789',' ')) b_3,
trim(translate(b4,'0123456789',' ')) b_4,
trim(translate(b5,'0123456789',' ')) b_5,
trim(translate(b6,'0123456789',' ')) b_6
from  temp_clean;

sim_b_rec cursor_sim_b%rowtype;

BEGIN
dbms_output.put_line('OUTSIDE the CURSOR ');

FOR sim_b_rec IN cursor_sim_b
LOOP
dbms_output.put_line('OUTSIDE the FOR  LOOP ');

FOR i IN 1..6
LOOP
ch := 'a_' ||i;
dbms_output.put_line('Inside the LOOP '||ch);


if sim_b_rec.ch is not null   -- error in passing 'ch' to cursor
then                          -- error says 'ch' not declared
BEGIN

execute immediate 'update temp_clean
set sim_b_rec.ch = NULL';

EXCEPTION
     WHEN  OTHERS THEN
     ROLLBACK;
END;

DBMS_OUTPUT.PUT_LINE('a'||i||'<' || sim_b_rec.ch || '>');
end if;

ch2 := 'b_' ||i;
if sim_b_rec.ch2 is not null
then
BEGIN
execute immediate 'update temp_clean
set sim_b_rec.ch2 = NULL';

EXCEPTION
     WHEN  OTHERS THEN
     ROLLBACK;
END;

DBMS_OUTPUT.PUT_LINE('b'||i||'<'|| sim_b_rec.ch2 || '>');
end if;

END LOOP;
END LOOP;



COMMIT;
END;
/ 

1 个答案:

答案 0 :(得分:0)

  

这不是答案。也许是OP的问题。

我猜你找到垃圾的方法不起作用。您正在将每个值转换为NULL。假设垃圾包含非数字。

见下文:

select translate('567','0123456789',NULL),
length(translate('567','0123456789',NULL) )
 from dual;
 -- OUTPUT: NULL NULL

select translate('abc56','0123456789',NULL) ,
length(translate('abc56','0123456789',NULL) )
from dual;
 -- OUTPUT: NULL NULL

select translate('%&12ab','0123456789',NULL),
length(translate('%&12ab','0123456789',NULL) )
 from dual;
   -- OUTPUT: NULL NULL

只是为了确保翻译的所有内容NULL已添加LENGTH支票。

为我们提供示例有效值和垃圾值以帮助您进一步

相关问题