From your code, there's no evidence that language.id
is automatically generated so I - while creating that table - set it to be an identity column.
If possible, get rid of double quotes for any kind of identifiers while working with Oracle; no benefit, only problems with that.
SQL> CREATE TABLE "LANGUAGE" (
2 "ID" NUMBER(5, 0) generated always as identity NOT NULL ENABLE,
3 "CODE" NVARCHAR2(5),
4 "DESCRIPTION" NVARCHAR2(50),
5 "IS_ACTIVE" NUMBER(1, 0) DEFAULT 1
6 );
Table created.
SQL> CREATE TABLE "Customer_Language" (
2 "ID" NUMBER(5, 0) NOT NULL ENABLE,
3 "CODE" NVARCHAR2(3),
4 "DESCRIPTION" VARCHAR2(50 BYTE),
5 "IS_ACTIVE" NUMBER(1, 0)
6 );
Table created.
In order to use the same ID value, one option is to switch to PL/SQL and use the returning clause:
SQL> declare
2 l_id number;
3 begin
4 insert into LANGUAGE (CODE,DESCRIPTION,IS_ACTIVE) values ('HI','Hindi','1')
5 returning id into l_id;
6
7 insert into "Customer_Language" (id, code, description, is_active)
8 values (l_id, 'HI', 'Hindi', '1');
9 end;
10 /
PL/SQL procedure successfully completed.
Result:
SQL> select * from language;
ID CODE DESCRIPTION IS_ACTIVE
---------- ----- -------------------------------------------------- ----------
1 HI Hindi 1
SQL> select * from "Customer_Language";
ID COD DESCRIPTION IS_ACTIVE
---------- --- -------------------------------------------------- ----------
1 HI Hindi 1
SQL>
As you can see, both ID
columns are the same.
[EDIT, related to your comments]
I currently don't have access to database that supports identity columns - this is Oracle 11g. I tried to simulate what you did, and code you posted (with some changes, obviously) works OK.
There are two users: scott
who will run PL/SQL code you posted and fetch/insert data over a database link; that DB link points to another user called mike
.
First, connected as mike
, I'm preparing environment:
SQL> connect mike/pwd@orcl
Connected.
SQL> create table user_tab_identity_cols as
2 select 'S695_LANGUAGE' table_name, 'SEQ_LANG' sequence_name from dual;
Table created.
SQL> create table language (code varchar2(10), description varchar2(15), is_active varchar2(1));
Table created.
SQL> create sequence seq_lang;
Sequence created.
SQL>
Connected as scott
, I'm running your code:
SQL> SET SERVEROUTPUT ON
SQL> CREATE TABLE customer_language
2 (
3 id NUMBER,
4 code VARCHAR2 (10),
5 description VARCHAR2 (15),
6 is_active VARCHAR2 (1)
7 );
Table created.
Procedure:
SQL> DECLARE
2 v_seq_name VARCHAR2 (20);
3 v_str VARCHAR2 (200);
4 v_val NUMBER;
5 BEGIN
6 SELECT sequence_name
7 INTO v_seq_name
8 FROM user_tab_identity_cols@dbl_mike
9 WHERE table_name = 'S695_LANGUAGE';
10
11 v_str := 'select ' || v_seq_name || '.nextval@dbl_mike from dual';
12 DBMS_OUTPUT.put_line ('v_str = ' || v_str);
13
14 EXECUTE IMMEDIATE v_str
15 INTO v_val;
16
17 DBMS_OUTPUT.put_line ('Value = ' || v_val);
18
19 INSERT INTO Language@dbl_mike (CODE, DESCRIPTION, IS_ACTIVE)
20 VALUES ('NL11', 'NEDERLANDS', '1');
21
22 INSERT INTO customer_LANGUAGE (id,
23 CODE,
24 DESCRIPTION,
25 IS_ACTIVE)
26 VALUES (v_val,
27 'NL11',
28 'NEDERLANDS',
29 '1');
30
31
32 DBMS_OUTPUT.put_line ('Value = ' || v_val);
33 COMMIT;
34 END;
35 /
v_str = select SEQ_LANG.nextval@dbl_mike from dual
Value = 4
Value = 4
PL/SQL procedure successfully completed.
Result: table over database link:
SQL> SELECT * FROM language@dbl_mike;
CODE DESCRIPTION I
---------- --------------- -
NL11 NEDERLANDS 1
Local table:
SQL> SELECT * FROM customer_language;
ID CODE DESCRIPTION I
---------- ---------- --------------- -
1 NL11 NEDERLANDS 1
SQL>
So: maybe it is about database version you use (check My Oracle Support for possible bug & solution (or patch)). Also, is there any database trigger involved? Because, there's no update
statement in code you posted, while Oracle complains about "distributed update operation failed". Did you try to rollback
before running that PL/SQL block (just in case)?