2014-12-06 67 views
1

使用Oracle中的過程我想從輸入參數更新表中所有記錄的代碼字段。例如,如果參數值是100:更新Oracle中列值的過程

與代碼256變化到101
第2行的代碼368變化至102
3行與代碼624變化至103

行1 I嘗試此

create or replace PROCEDURE procedure_name (parameter_newcode code%TYPE) AS 
CURSOR cursor_name IS select code from table 
          order by code asc; 
    var_newCode code%TYPE; 
    BEGIN 
    OPEN cursor_name; 
    LOOP 
    FETCH cursor_name INTO var_newCode; 
    EXIT WHEN cursor_name%NOTFOUND; 
    var_newCode:=parameter_newcode+1; 
    update table set code = var_newCode; 
    END LOOP; 
    CLOSE cursor_name; 
END procedure_name; 

但返回相同的代碼的每一行(例如,如果該參數爲100 - > 101)

回答

2

在每個循環要設置(連連)
var_newCode:=parameter_newcode+1;
和你沒有指定,要更新的記錄
update table set code = var_newCode;

沿東西

create or replace PROCEDURE procedure_name (parameter_newcode code%TYPE) AS 
CURSOR cursor_name IS select code from myTable 
          order by code asc; 
    var_oldCode code%TYPE; 
    var_newCode code%TYPE; 
    BEGIN 
    var_newCode:=parameter_newcode; 
    OPEN cursor_name; 
    LOOP 
    FETCH cursor_name INTO var_oldCode; 
    EXIT WHEN cursor_name%NOTFOUND; 
    var_newCode:=var_newCode+1; 
    update myTable set code = var_newCode where code = var_oldCode; 
    END LOOP; 
    CLOSE cursor_name; 
END procedure_name; 

應該工作。 (對不起,現在不能測試。)