PL / SQL用于增强表的多个更新语句的过程

时间:2016-04-18 08:30:42

标签: sql oracle plsql

我需要使用随机值更新数据库中的一个表,以便我编写了多个更新语句,但是它需要花费大量时间来执行,我需要更新表中包含100个列的15个列列。有人可以帮助我为以下语句编写PL / SQL过程。我有15列要更新。我用可变值字段

写了要更新的列的ID号

先谢谢你。

UPDATE MY_Table
 SET COL1O=DBMS_RANDOM.STRING('A', DBMS_RANDOM.VALUE(10, 15)) 
 where TRIM(COL1) IS NOT NULL ;

UPDATE MY_Table
 SET COL11=DBMS_RANDOM.STRING('A', DBMS_RANDOM.VALUE(10, 15)) 
 where TRIM(COL2) IS NOT NULL ;

UPDATE MY_Table
 SET COL12=DBMS_RANDOM.STRING('A', DBMS_RANDOM.VALUE(8, 15)) 
 where TRIM(COL3) IS NOT NULL ;

UPDATE MY_Table
 SET COL13=DBMS_RANDOM.STRING('A', DBMS_RANDOM.VALUE(8, 15)) 
 where TRIM(COL4) IS NOT NULL ;

UPDATE MY_Table
 SET COL14=DBMS_RANDOM.STRING('A', DBMS_RANDOM.VALUE(8, 15)) 
  where TRIM(COL5) IS NOT NULL;

UPDATE MY_Table
 SET COL18=DBMS_RANDOM.STRING('A', DBMS_RANDOM.VALUE(1, 2)) 
  where TRIM(COL18) IS NOT NULL;

UPDATE MY_Table
 SET COL22=DBMS_RANDOM.VALUE(1, 1)) 
  where TRIM(COL22) IS NOT NULL; 

 UPDATE MY_Table
 SET COL37=DBMS_RANDOM.VALUE(1, 5)) 
  where TRIM(COL37) IS NOT NULL; 

 UPDATE MY_Table
 SET COL114=DBMS_RANDOM.VALUE(8, 10)) 
  where TRIM(COL114) IS NOT NULL; 

 UPDATE MY_Table
 SET COL140=DBMS_RANDOM.VALUE(8, 10)) 
  where TRIM(COL140) IS NOT NULL; 

 UPDATE MY_Table
 SET COL141=DBMS_RANDOM.VALUE(5, 15)) 
  where TRIM(COL141) IS NOT NULL; 

 UPDATE MY_Table
 SET COL145=DBMS_RANDOM.VALUE(8, 11)) 
  where TRIM(COL145) IS NOT NULL; 

UPDATE MY_Table
 SET COL192=DBMS_RANDOM.VALUE(0.00, 9999999999999.00) 
  where TRIM(COL114) IS NOT NULL; 

UPDATE MY_Table
 SET COL193=DBMS_RANDOM.VALUE(0.00, 9999999999999.00) 
  where TRIM(COL114) IS NOT NULL; 

UPDATE MY_Table
 SET COL195=DBMS_RANDOM.VALUE(0.00, 9999999999999.00)
  where TRIM(COL114) IS NOT NULL; 

UPDATE MY_Table
 SET COL114=DBMS_RANDOM.VALUE(5, 24)) 
  where TRIM(COL114) IS NOT NULL; 

2 个答案:

答案 0 :(得分:1)

您不需要15个更新语句,您可以在一个语句中执行此操作:

UPDATE MY_Table
 SET COL1 = case 
             when TRIM(COL1) IS NOT NULL then  DBMS_RANDOM.STRING('A', DBMS_RANDOM.VALUE(10, 15)) 
             else col1
           end,
     COL3 = case 
              when TRIM(COL3) IS NOT NULL then DBMS_RANDOM.STRING('A', DBMS_RANDOM.VALUE(8, 15)) 
              else col3
            end,
     COL15 = case
               when where TRIM(COL15) IS NOT NULL  then DBMS_RANDOM.STRING('A', DBMS_RANDOM.VALUE(5, 15))        
               else col15
             end

如果您有许多行不符合条件,添加where条件可以加快速度

update my_table
  set ....
where (TRIM(COL1) IS NOT NULL or
       TRIM(COL3) IS NOT NULL or
       TRIM(COL15) IS NOT NULL)

答案 1 :(得分:0)

您可以使用动态SQL,如:

begin
    for i in (select n,
                    case 
                      when n = 1 then 'DBMS_RANDOM.STRING(''A'', DBMS_RANDOM.VALUE(10, 15))'
                      when n = 10 then '...',
                      ...
                      else '...'
                    end as val
              from (
                    select level as n
                    from dual
                    connect by level <= 91
                ) where n in (10, 20)
              )
    loop

        execute immediate 'UPDATE MY_Table
                             SET COL' || i.n || '=' || i.val || '
                             where TRIM(COL' || i.n || ') IS NOT NULL';
    end loop;
end;