如何使用相同的IDENTITY()值

时间:2017-03-02 17:30:02

标签: sql h2

以下SQL在H2上执行时会产生意外结果:

create table TB_RFD_COUNTRY (
    ID bigint generated by default as identity,
    CREATED_DATE timestamp not null,
    MODIFIED_DATE timestamp,
    ALPHA_CODE_2 varchar(255) not null,
    ALPHA_CODE_3 varchar(255) not null,
    NUMERIC_CODE integer not null,
    primary key (ID)
);

create table TB_RFD_COUNTRY_VALUE (
    ID bigint generated by default as identity,
    CREATED_DATE timestamp not null,
    MODIFIED_DATE timestamp,
    LANGUAGE varchar(2) not null,
    VALUE varchar(255) not null,
    COUNTRY_ID bigint,
    primary key (ID)
);

INSERT INTO TB_RFD_COUNTRY (ALPHA_CODE_2, ALPHA_CODE_3, NUMERIC_CODE, CREATED_DATE) VALUES ('AF', 'AFG', '4', current_timestamp);
INSERT INTO TB_RFD_COUNTRY_VALUE (LANGUAGE, VALUE, COUNTRY_ID, CREATED_DATE) VALUES
  ('en', 'Afghanistan', IDENTITY(), current_timestamp),
  ('fr', 'Afghanistan (l'')', IDENTITY(), current_timestamp);

INSERT INTO TB_RFD_COUNTRY (ALPHA_CODE_2, ALPHA_CODE_3, NUMERIC_CODE, CREATED_DATE) VALUES ('AX', 'ALA', '248', current_timestamp);
INSERT INTO TB_RFD_COUNTRY_VALUE (LANGUAGE, VALUE, COUNTRY_ID, CREATED_DATE) VALUES
  ('en', 'Åland Islands', IDENTITY(), current_timestamp),
  ('fr', 'Åland(les Îles)', IDENTITY(), current_timestamp);

INSERT INTO TB_RFD_COUNTRY (ALPHA_CODE_2, ALPHA_CODE_3, NUMERIC_CODE, CREATED_DATE) VALUES ('AL', 'ALB', '8', current_timestamp);
INSERT INTO TB_RFD_COUNTRY_VALUE (LANGUAGE, VALUE, COUNTRY_ID, CREATED_DATE) VALUES
  ('en', 'Albania', IDENTITY(), current_timestamp),
  ('fr', 'Albanie (l'')', IDENTITY(), current_timestamp);

INSERT INTO TB_RFD_COUNTRY (ALPHA_CODE_2, ALPHA_CODE_3, NUMERIC_CODE, CREATED_DATE) VALUES ('DZ', 'DZA', '12', current_timestamp);
INSERT INTO TB_RFD_COUNTRY_VALUE (LANGUAGE, VALUE, COUNTRY_ID, CREATED_DATE) VALUES
  ('en', 'Algeria', IDENTITY(), current_timestamp),
  ('fr', 'Algérie (l'')', IDENTITY(), current_timestamp);

第一个IDENTITY()调用返回NULL。第二个调用返回TB_RFD_COUNTRY_VALUE中插入的英文翻译的ID。

这种SQL语句在MySQL或HSQLDB下工作。有没有办法让它们在H2下工作,还是我需要将IDENTITY()结果存储在变量中?

0 个答案:

没有答案