473,609 Members | 2,222 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

trigger question

I am using db2 udb v8.2 AIX

I have created trigger, however I am not confident it meets industry
standards. If I make it fail, I cant tell from the message where it is
failing. what can I add to trap the errors properly?
CREATE TRIGGER myschema.t1_upd _t
AFTER UPDATE OF dt ON myschema.t1
REFERENCING NEW AS N
FOR EACH ROW mode db2sql

when (N.dt is not null)
BEGIN ATOMIC

DECLARE rs INTEGER DEFAULT 0;

insert into myschema.audit
select t2.* from myschema.t2
where t2.id = N.id
and t2.dt = N.dt
and t2.cd = 'X';

insert into myschema.audit2
select t2.* from myschema.t2
where t2.id = N.id
and t2.dt = N.dt
and t2.cd = 'X';
GET DIAGNOSTICS rs = RETURN_STATUS;
values 'failed';
end@
Nov 12 '05 #1
4 4172
What error are you getting?

the following two lines won't do anything:
GET DIAGNOSTICS rs = RETURN_STATUS;
values 'failed';
What do you want them to do?

If a trigger in DB2 for LUW encounters an error the whole statement fails.
Use the SIGNAL statement to raise an error.
If you want to do compelx logic, including full condition handling use
the CALL statement in DB2 V8.2 to call out to a store procedure.
The stored procedure can do the heavy lifting for the trigger.

Cheers
Serge
Nov 12 '05 #2
Serge Rielau <sr*****@ca.ibm .com> wrote in message news:<2t******* ******@uni-berlin.de>...
What error are you getting?

the following two lines won't do anything:
GET DIAGNOSTICS rs = RETURN_STATUS;
values 'failed';
What do you want them to do?

If a trigger in DB2 for LUW encounters an error the whole statement fails.
Use the SIGNAL statement to raise an error.
If you want to do compelx logic, including full condition handling use
the CALL statement in DB2 V8.2 to call out to a store procedure.
The stored procedure can do the heavy lifting for the trigger.

Cheers
Serge


now I recall why I put the 'heavy lifting' in the trigger - I could
not get it to call the proc. I created a generic set of objects and
got the same error.
calling the proc works on its own but not when the trigger fires:

try this:

drop table hist2003;
drop table hist2004;
drop table hist2005;
drop view hist;

drop table archivesource;
drop trigger source_upd_t;

drop table source1;
drop table sourceopen;

drop procedure archive_sp;

create table hist2003(cola integer not null, colb integer not null,
pay_dt timestamp , closed_dt timestamp );
ALTER TABLE hist2003 ADD CONSTRAINT hist2003_chk CHECK (year(pay_dt) =
2003);

create table hist2004(cola integer not null, colb integer not null,
pay_dt timestamp not null, closed_dt timestamp not null );
ALTER TABLE hist2004 ADD CONSTRAINT hist2004_chk CHECK (year(pay_dt) =
2004);

create table hist2005(cola integer not null, colb integer not null,
pay_dt timestamp not null, closed_dt timestamp not null );
ALTER TABLE hist2005 ADD CONSTRAINT hist2005_chk CHECK (year(pay_dt) =
2005);

create view hist (cola, colb, pay_dt, closed_dt) as
select cola, colb, pay_dt, closed_dt from hist2003
union all
select cola, colb, pay_dt, closed_dt from hist2004
union all
select cola, colb, pay_dt, closed_dt from hist2005;
create table source1 (cola integer not null, colb integer not null,
pay_dt timestamp not null, closed_dt timestamp );
create table archivesource (cola integer not null, colb integer not
null, open_dt timestamp not null);

create table sourceopen (cola integer not null, colb integer not null,
open_dt timestamp not null);
insert into source1
values
(100, 5555, '2003-01-01-00.00.00.000000 ',null),
(100, 5556, '2004-01-02-00.00.00.000000 ',null),
(100, 5557, '2005-01-03-00.00.00.000000 ',null);
insert into sourceopen
values
(100, 5555, '2005-01-03-00.00.00.000000 '),
(100, 5556, '2005-01-03-00.00.00.000000 '),
(100, 5557, '2005-01-03-00.00.00.000000 ');

create procedure archive_sp (IN custid integer, IN orderid integer)
Language SQL
Begin
insert into archivesource
select * from sourceopen
where cola = custid
and colb = orderid;
insert into hist
select cola, colb, pay_dt, closed_dt
from source1
where cola = custid
and colb = orderid;
end@

CREATE TRIGGER source_upd_t
AFTER UPDATE OF closed_dt ON source1
REFERENCING NEW AS N
FOR EACH ROW mode db2sql
when (N.closed_dt is not null)
begin atomic
call archive_sp (N.cola, N.colb);
end@
--Test#1:confirm procedure works on its own
delete from hist;
delete from archivesource;
call archive_sp (100,5555);
select * from hist;
select * from archivesource;

--Test#2: confirm update action fires trigger, which calls proc
--confirm hist and archive tables are empty prior to running trigger
fire
delete from hist;
delete from archivesource;

update source1 set closed_dt = current timestamp where colb in (5555,
5556);

--confirm 1 row in each table
select * from hist;
select * from archivesource;
update source1 set closed_dt = current timestamp where colb in (5555,
5556)
DB21034E The command was processed as an SQL statement because it was
not a
valid Command Line Processor command. During SQL processing it
returned:
SQL0723N An error occurred in a triggered SQL statement in trigger
"SOURCE_". Information returned for the error includes SQLCODE
"-746", SQLSTATE "57053" and message tokens
"ARCHIVE_SP|SQL 041020162010682 |REA". SQLSTATE=09000
Nov 12 '05 #3
Why do you select from the trigger target?
Simply pass in what you need through the procedure.
You may get a few parameters in your real life scenario, but that will
solve your problem.

Cheers
Serge
Nov 12 '05 #4
Serge Rielau <sr*****@ca.ibm .com> wrote in message news:<2t******* ******@uni-berlin.de>...
Why do you select from the trigger target?
Simply pass in what you need through the procedure.
You may get a few parameters in your real life scenario, but that will
solve your problem.

Cheers
Serge


Serge, I changed the trigger to not select from the source table.
it worked.

thx,
ap
create procedure archive_sp (IN custid integer, IN orderid integer, IN
paymdt timestamp, IN closedt timestamp)
Language SQL
Begin
FOR z_cursor AS
SELECT source_id,cola, colb, open_dt
FROM sourceopen s
WHERE s.cola = custid
AND s.colb = orderid
DO

insert into archivesource values
(z_cursor.sourc e_id,z_cursor.c ola, z_cursor.colb, z_cursor.open_d t)
;
insert into hist values (z_cursor.cola, z_cursor.colb, paymdt,
closedt) ;
END FOR;
end@
Nov 12 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
8081
by: Joel Thornton | last post by:
Whenever something is inserted to a given table, I want to run some shell commands using xp_cmdshell. Would it be a bad idea to put this xp_cmdshell in the INSERT trigger of this table? I understand that when using xp_cmdshell, the sql thread in question waits until xp_cmdshell finishes what it's doing. Does this mean if my xp_cmdshell call takes 30 seconds, that nobody else can insert to this table until my xp_cmdshell and rest of the...
1
1996
by: Matik | last post by:
Hello to all, I have a small question. I call the SP outer the DB. The procedure deletes some record in table T1. The table T1 has a trigger after delete. This is very importand for me, that the SP will be finished ASAP, that's why, I do not want, and I do not need to wait for a trigger.
6
6543
by: Scott CM | last post by:
I have a multi-part question regarding trigger performance. First of all, is there performance gain from issuing the following within a trigger: SELECT PrimaryKeyColumn FROM INSERTED opposed to: SELECT * FROM INSERTED
2
2736
by: robert | last post by:
typed this into the ibm.com search window, but didn't get anything that looked like it would answer a question: +trigger +faster +db2 +cobol the question: are DB2 (390/v6, at the moment) triggers better or worse (and how much so, of course) than the same functionality coded in COBOL? assuming, of course, equal competence in the code.
0
7134
by: Dave Sisk | last post by:
I've created a system or external trigger on an AS/400 file a.k.a DB2 table. (Note this is an external trigger defined with the ADDPFTRG CL command, not a SQL trigger defined with the CREATE TRIGGER statement.) I've also defined a SQL stored proc, and the trigger is set to call this SP. I've posted the simplified source below. I can manually call the stored proc, and the external trigger is created without any errors. However, when I do...
5
3290
by: Bob Stearns | last post by:
I have two (actually many) dates in a table I want to validate on insertion. The following works in the case of only one WHEN clause but fails with two (or more), with the (improper? inappropriate?) error message: SQLCODE: -104, SQLSTATE: 42601, SQLERRMC: CREATE TRIGGER IS3.date_later_001i NO C;BEGIN-OF-STATEMENT;<space> which is interpreted as: An unexpected token "CREATE TRIGGER IS3.date_later_001i NO C" was found
3
4941
by: ChrisN | last post by:
Hello all, I have a quick question. I'm using a C# object to commit new rows to a database. In the database I have an INSERT Trigger watching values come in. If the record to be committed fails the trigger's test, the trigger rolls back the INSERT command and no changes are made to the database. As far as my object is concerned, the transaction went through either way (no matter what the trigger did). What I need is for the object...
3
3719
by: teddysnips | last post by:
I need a trigger (well, I don't *need* one, but it would be optimal!) but I can't get it to work because it references ntext fields. Is there any alternative? I could write it in laborious code in the application, but I'd rather not! DDL for table and trigger below. TIA
9
9303
by: Ots | last post by:
I'm using SQL 2000, which is integrated with a VB.NET 2003 app. I have an Audit trigger that logs changes to tables. I want to apply this trigger to many different tables. It's the same trigger, with the exception of the table name. I could manually change the table name in the trigger and create it, over and over, but I'd like to automate this - by iterating through the collection of tables and passing the tablename to something that...
10
3561
by: JohnO | last post by:
Hi All, This question is related to iSeries V5R4 and db2. I want to implement an AFTER DELETE trigger to save the deleted rows to an archive table, I initially defined it as a FOR EACH STATEMENT trigger that would insert all the deleted rows in one operation like this: CREATE TRIGGER MyTable_TD
0
8053
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8205
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8380
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6983
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4007
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4066
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2519
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1638
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1374
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.