将update语句转换为数组中的值

时间:2014-06-14 10:33:47

标签: sql oracle plsql oracle11g procedure

我正在尝试将长更新脚本转换为循环数组的过程,并在nececary时更改

旧剧本:

create or replace PROCEDURE Location_Name_Routine is

BEGIN
    DELETE
        FROM Location
        WHERE Name LIKE '%[^0-9a-zA-Z"]%';
  update Location
      set Name = nls_initcap(Name, 'NLS_SORT=xDutch');
    UPDATE Location
        SET  Name = REGEXP_REPLACE(Name,' En',' en');
    UPDATE Location
        SET  Name = REGEXP_REPLACE(Name,' Van',' van');
    UPDATE Location
        SET  Name = REGEXP_REPLACE(Name,' De',' de');
    UPDATE Location
        SET  Name = REGEXP_REPLACE(Name,' Den',' den');
    UPDATE Location
        SET  Name = REGEXP_REPLACE(Name,' Over',' over');
    UPDATE Location
        SET  Name = REGEXP_REPLACE(Name,' Aan',' aan');   
    UPDATE Location
        SET  Name = REGEXP_REPLACE(Name,' Bij',' bij');
    UPDATE Location
        SET  Name = REGEXP_REPLACE(Name,'''S', '''s');
END;
/

必须成为:

set serveroutput on;
DECLARE
   type array_t is varray(7) of varchar2(10);
   array array_t := array_t(' De', ' Van', ' Den',' Over', ' Aan', ' Bij','''S' );
begin
   for i in 1..array.count loop
    UPDATE Location
        SET  Name = REGEXP_REPLACE(Name, array_t , lower(array_t));
    end loop;
end;

在某种程度上,无论我尝试什么,它都无法工作。 所以,如果有人有一些消化,请告诉他们,我会立即尝试。

1 个答案:

答案 0 :(得分:1)

评论太长了。你的陈述:

DELETE
    FROM Location
    WHERE Name LIKE '%[^0-9a-zA-Z"]%';

至少有两个问题。首先,您使用的是like模式的SQL Server约定,而不是Oracle约定。您应该用正则表达式替换它:

DELETE FROM LOCATION
    WHERE NOT REGEXP_LIKE(Name, '[^0-9A-Za-z'']');

然后,我不会在PL / SQL中使用数组,但我认为你需要一个索引:

UPDATE Location
    SET  Name = REGEXP_REPLACE(Name, array_t(i), lower(array_t(i)));
end loop;