If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Triggers and SQLSTATE 428F9

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-10-09, 07:40
bigmickey bigmickey is offline
Registered User
 
Join Date: Nov 2009
Posts: 3
Triggers and SQLSTATE 428F9

Hello,

I have problem with triggers. There are two triggers: before (abi) and after (aai) insert. The signalled error is in aai with tokens from abi Both triggers should work separately and should know nothing about each other. I know there are some exceptions when NEXT VALUE cannot be used but it seems it does not cover my problem. What is wrong and how to solve it then?
(environment: db2 9.5 with no patches on Linux)

create table a (i integer)!
create table b (i integer)!
CREATE SEQUENCE sq AS integer!
CREATE TRIGGER abi BEFORE INSERT ON a REFERENCING NEW AS N FOR EACH ROW MODE DB2SQL
BEGIN ATOMIC
SET N.i = NEXTVAL FOR sq;
END!
CREATE TRIGGER aai AFTER INSERT ON a REFERENCING NEW AS N FOR EACH ROW MODE DB2SQL
BEGIN ATOMIC
insert into b (i)
WITH x(xi) AS (values N.i UNION ALL SELECT xi+1 FROM x WHERE xi<N.i+10)
SELECT xi FROM x;
END!
insert into a values (0)!

Error: SQL0723N An error occurred in a triggered SQL statement in trigger "DB2INST1.AAI". Information returned for the error includes SQLCODE "-348", SQLSTATE "428F9" and message tokens "NEXTVAL FOR DB2INST1.SQ"

If we replace a recursive with simple "insert into b (i) values (n.i);" or remove any trigger it will be working properly. Unfortunately on production I have to have a sequence call before and recursive query after insert.

Last edited by bigmickey; 11-10-09 at 08:09.
Reply With Quote
  #2 (permalink)  
Old 11-10-09, 08:05
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
I think you can try to look at the INSERT explain plan to see what is it that DB2 is trying to execute.
Reply With Quote
  #3 (permalink)  
Old 11-12-09, 05:42
bigmickey bigmickey is offline
Registered User
 
Join Date: Nov 2009
Posts: 3
Unfortunately the explain plan in such situation is not accessible. I've got the same error.
Any other ideas? Is it known problem? Maybe it is an additional nonsensical exception in DB2 making the programmers to be cross?
Reply With Quote
  #4 (permalink)  
Old 11-12-09, 06:42
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
Appears to be the way it will/should work ... Sequences are not supported in recursive CTEs.

Open a PMR and confirm with IBM ...

The workaround is to use a Stored Proc in the After Trigger

Cheers
Sathyaram
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.

Last edited by sathyaram_s; 11-12-09 at 06:46.
Reply With Quote
  #5 (permalink)  
Old 11-12-09, 07:04
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
It looks worked well on my DB2 9.7 for Windows.

Code:
------------------------------ Commands Entered ------------------------------
connect to SAMPLE ;
------------------------------------------------------------------------------

   Database Connection Information

 Database server        = DB2/NT 9.7.0
 SQL authorization ID   = DB2ADMIN
 Local database alias   = SAMPLE


A JDBC connection to the target has succeeded.
------------------------------ Commands Entered ------------------------------
create table a (i integer)!
------------------------------------------------------------------------------
DB20000I  The SQL command completed successfully.

------------------------------ Commands Entered ------------------------------
create table b (i integer)!
------------------------------------------------------------------------------
DB20000I  The SQL command completed successfully.

------------------------------ Commands Entered ------------------------------
CREATE SEQUENCE sq AS integer!
------------------------------------------------------------------------------
DB20000I  The SQL command completed successfully.

------------------------------ Commands Entered ------------------------------
CREATE TRIGGER abi BEFORE INSERT ON a REFERENCING NEW AS N FOR EACH ROW MODE DB2SQL 
BEGIN ATOMIC
SET N.i = NEXTVAL FOR sq;
END!
------------------------------------------------------------------------------
DB20000I  The SQL command completed successfully.

------------------------------ Commands Entered ------------------------------
CREATE TRIGGER aai AFTER INSERT ON a REFERENCING NEW AS N FOR EACH ROW MODE DB2SQL 
BEGIN ATOMIC 
insert into b (i)
WITH x(xi) AS (values N.i UNION ALL SELECT xi+1 FROM x WHERE xi<N.i+10) 
SELECT xi FROM x;
END!
------------------------------------------------------------------------------
SQL0347W  The recursive common table expression "DB2ADMIN.X" may contain an 
infinite loop.  SQLSTATE=01605

------------------------------ Commands Entered ------------------------------
insert into a values (0)!
------------------------------------------------------------------------------
DB20000I  The SQL command completed successfully.

------------------------------ Commands Entered ------------------------------
SELECT * FROM a!
------------------------------------------------------------------------------

I          
-----------
          1

  1 record(s) selected.


------------------------------ Commands Entered ------------------------------
SELECT * FROM b!
------------------------------------------------------------------------------

I          
-----------
          1
          2
          3
          4
          5
          6
          7
          8
          9
         10
         11

  11 record(s) selected.
Reply With Quote
  #6 (permalink)  
Old 11-12-09, 11:21
bigmickey bigmickey is offline
Registered User
 
Join Date: Nov 2009
Posts: 3
Thank you to both of you. The workaround is working and as for now it's the only solution in my case.
Reply With Quote
  #7 (permalink)  
Old 11-12-09, 12:10
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
That's interesting ... Can you post the explain plan please, if at all possible ...



Quote:
Originally Posted by tonkuma View Post
It looks worked well on my DB2 9.7 for Windows.

Code:
------------------------------ Commands Entered ------------------------------
connect to SAMPLE ;
------------------------------------------------------------------------------

   Database Connection Information

 Database server        = DB2/NT 9.7.0
 SQL authorization ID   = DB2ADMIN
 Local database alias   = SAMPLE


A JDBC connection to the target has succeeded.
------------------------------ Commands Entered ------------------------------
create table a (i integer)!
------------------------------------------------------------------------------
DB20000I  The SQL command completed successfully.

------------------------------ Commands Entered ------------------------------
create table b (i integer)!
------------------------------------------------------------------------------
DB20000I  The SQL command completed successfully.

------------------------------ Commands Entered ------------------------------
CREATE SEQUENCE sq AS integer!
------------------------------------------------------------------------------
DB20000I  The SQL command completed successfully.

------------------------------ Commands Entered ------------------------------
CREATE TRIGGER abi BEFORE INSERT ON a REFERENCING NEW AS N FOR EACH ROW MODE DB2SQL 
BEGIN ATOMIC
SET N.i = NEXTVAL FOR sq;
END!
------------------------------------------------------------------------------
DB20000I  The SQL command completed successfully.

------------------------------ Commands Entered ------------------------------
CREATE TRIGGER aai AFTER INSERT ON a REFERENCING NEW AS N FOR EACH ROW MODE DB2SQL 
BEGIN ATOMIC 
insert into b (i)
WITH x(xi) AS (values N.i UNION ALL SELECT xi+1 FROM x WHERE xi<N.i+10) 
SELECT xi FROM x;
END!
------------------------------------------------------------------------------
SQL0347W  The recursive common table expression "DB2ADMIN.X" may contain an 
infinite loop.  SQLSTATE=01605

------------------------------ Commands Entered ------------------------------
insert into a values (0)!
------------------------------------------------------------------------------
DB20000I  The SQL command completed successfully.

------------------------------ Commands Entered ------------------------------
SELECT * FROM a!
------------------------------------------------------------------------------

I          
-----------
          1

  1 record(s) selected.


------------------------------ Commands Entered ------------------------------
SELECT * FROM b!
------------------------------------------------------------------------------

I          
-----------
          1
          2
          3
          4
          5
          6
          7
          8
          9
         10
         11

  11 record(s) selected.
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On