load自增字段的处理


5.处理数据库表中的自增字段

对于需要加载的含有自增字段的表,即该表的 ixf 数据文件中有自增列的值, 可以在 load 命令中加入如下参数控制自增字段值:
1). modified by identityignore :加载的数据文件中有自增字段值,load 时忽略数据文件中自增字段值 ;
2). modified by identitymissing :加载的数据文件中没有自增字段值,load 时自动生成自增字段值 ;
3). modified by identityoverride :加载的数据文件中有自增字段值,load 时使用数据文件中的自增字段值 。


为了使目标数据库中含有自增字段的表中数据与源数据库中的数据保持一致,本文实例中选择使用 modified by identityoverride 参数,在导入数据时使用数据文件中的自增字段值。读者可以根据不同情况选择适当的控制参数。

for example:
   db2 load from test.ixf of ixf modified by identityoverride insert into TEST;


CREATE PROCEDURE "ARS"."CREATE_IDENT"
(OUT "O_MSG" VARCHAR(500)
)
  SPECIFIC "ARS"."CREATE_IDENT"
  LANGUAGE SQL
  NOT DETERMINISTIC
  CALLED ON NULL INPUT
  EXTERNAL ACTION
  OLD SAVEPOINT LEVEL
  MODIFIES SQL DATA
  INHERIT SPECIAL REGISTERS
  begin
  
     declare v_max bigint;
declare v_sql varchar(500) default '';
DECLARE GLOBAL TEMPORARY TABLE SESSION.v_int
     (
      max_id decimal(22,0)
     )ON COMMIT PRESERVE ROWS NOT LOGGED WITH REPLACE;
     
for v_cur as select name,TBNAME from SYSIBM.SYSCOLUMNS where  IDENTITY = 'Y' and TBCREATOR = 'ARS'
do
     delete from SESSION.v_int;
     set v_sql = ' insert into SESSION.v_int values(' ||'select coalesce(max('||v_cur.name||')+1,0) from '||v_cur.tbname||')';
set o_msg = v_Sql;
execute immediate v_sql;
select max_id into v_max from SESSION.v_int;
set v_sql =' alter table  '||v_cur.tbname||' alter ' ||v_cur.name||' restart with '||rtrim(ltrim(char(v_max)));
         execute immediate v_sql;
         set o_msg = v_Sql;
end for;
end;


---syscolumns 列数据。
select * from SYSIBM.SYSCOLUMNS where identity='Y';

---更改自增列。
  alter table ars.test alter id restart with 8;
 

你可能感兴趣的:(sql,db2)