As a follow up to an earlier discussion in this group on triggers and
federated tables. This relates to the problem that I'm trying to solve,
so I'm posting it in this thread:
It's easy to set up a server for two-phase commit so that one can update
both databases in the same transaction:
connect to s ;
create table t1
( x int not null primary key
, y int not null ) ;
connect to m ;
create table t1
( x int not null primary key
, y int not null ) ;
CREATE WRAPPER DRDA
OPTIONS( DB2_FENCED 'N');
CREATE SERVER S TYPE DB2/UDB VERSION 9
WRAPPER DRDA
AUTHORIZATION "db2inst1" PASSWORD "********"
OPTIONS( DBNAME 'S', PASSWORD 'Y', DB2_TWO_PHASE_COMMIT 'Y' );
CREATE USER MAPPING FOR "db2inst1" SERVER "S"
OPTIONS ( REMOTE_AUTHID 'db2inst1', REMOTE_PASSWORD '*******');
CREATE NICKNAME S.T1
FOR s.db2inst1.t1;
db2 +c "insert into t1(x,y) values (1,1)"
DB20000I The SQL command completed successfully.
[db2inst1@nya-09 ~/nobackup/ltjn/tek122/federation]$ db2 +c "insert into
s.t1(x,y) values (1,1)"
DB20000I The SQL command completed successfully.
[db2inst1@nya-09 ~/nobackup/ltjn/tek122/federation]$ db2 commit
DB20000I The SQL command completed successfully.
alles gut. However, it is not possible to directly create a trigger on
t1 that updates s.t1:
SQL30090N Operation invalid for application execution environment. Reason
code = "22". LINE NUMBER=3. SQLSTATE=25000
Not sure what tha rationale behind this is. Here is a - ugly - way of
overcoming this restriction:
create procedure db2inst1.proc (in_x int, in_y int)
language sql
begin atomic
declare stmt varchar(100);
set stmt = 'insert into s.t1 (x,y) values (' ||
char(in_x) || ',' || char(in_y) || ')';
execute immediate stmt;
end @
create trigger db2inst1.trg
after insert on db2inst1.t1
referencing new as n
for each row
call db2inst1.proc(n.x, n.y)
@
[db2inst1@nya-09 ~/nobackup/ltjn/tek122/federation]$ db2 +c "insert into
s.t1(x,y) values (2,2)"
DB20000I The SQL command completed successfully.
[db2inst1@nya-09 ~/nobackup/ltjn/tek122/federation]$ db2 commit
DB20000I The SQL command completed successfully.
[db2inst1@nya-09 ~/nobackup/ltjn/tek122/federation]$ db2 "select * from
s.t1"
X Y
----------- -----------
1 1
2 2
2 record(s) selected.
/Lennart