473,405 Members | 2,279 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Multiple-condition trigger trouble

Hi experts,

I am having a hard time coding a trigger to handle multiple validation
conditions in DB2 UDB V8.1.9.. I thought that a single trigger might be
better than one-trigger-per-condition, but so far, no cigar. Here is
what I'd like to do (pseudo-code):

CREATE TRIGGER fp_insert
NO CASCADE BEFORE INSERT
ON DB2INST1.FILE_PROPERTY
REFERENCING NEW AS n
FOR EACH ROW
BEGIN ATOMIC
WHEN (n.property_id = 175 and not exists (select 1 from
DICT_TOO_TRUTH where dict_value = n.property_value))
THEN RAISE_ERROR('70001', 'Error')
WHEN (n.property_id = 187 and not exists (select 1 from
DICT_BIOPSY_TRUTH where dict_value = n.property_value))
THEN RAISE_ERROR('70001', 'Error')
END;

DB2 does not like multiple "WHEN" conditions and I cannot find the
correct syntax for CASE. Can anyone help?

Thanks!

Alejandrina

Dec 1 '06 #1
7 8966
Create syntax you have there ... :-)

CREATE TRIGGER fp_insert
NO CASCADE BEFORE INSERT
ON DB2INST1.FILE_PROPERTY
REFERENCING NEW AS n
FOR EACH ROW
WHEN (n.property_id = 175
and not exists (select 1 from DICT_TOO_TRUTH
where dict_value = n.property_value)
OR
n.property_id = 187
and not exists (select 1 from DICT_BIOPSY_TRUTH
where dict_value = n.property_value))
VALUES(RAISE_ERROR('70001', 'Error'));

The trigger condition precedes the trigger body.

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab

WAIUG Conference
http://www.iiug.org/waiug/present/Fo...Forum2006.html
Dec 1 '06 #2
apattin wrote:
Hi experts,

I am having a hard time coding a trigger to handle multiple validation
conditions in DB2 UDB V8.1.9.. I thought that a single trigger might be
better than one-trigger-per-condition, but so far, no cigar. Here is
what I'd like to do (pseudo-code):

CREATE TRIGGER fp_insert
NO CASCADE BEFORE INSERT
ON DB2INST1.FILE_PROPERTY
REFERENCING NEW AS n
FOR EACH ROW
BEGIN ATOMIC
WHEN (n.property_id = 175 and not exists (select 1 from
DICT_TOO_TRUTH where dict_value = n.property_value))
THEN RAISE_ERROR('70001', 'Error')
WHEN (n.property_id = 187 and not exists (select 1 from
DICT_BIOPSY_TRUTH where dict_value = n.property_value))
THEN RAISE_ERROR('70001', 'Error')
END;

DB2 does not like multiple "WHEN" conditions and I cannot find the
correct syntax for CASE. Can anyone help?
You could resort to IF ... THEN ... ELSE ... or a CASE statement. With the
latter, you need to put an CASE ... END CASE around the stuff in the atomic
compound.

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Dec 1 '06 #3
Thanks Serge,

The solution you gave me works only if the trigger action is the same
for all cases, which I'd rather not have. My mistake, because I should
have said that I wanted to implement the following logic:

CREATE TRIGGER fp_insert
NO CASCADE BEFORE INSERT
ON DB2INST1.FILE_PROPERTY
REFERENCING NEW AS n
FOR EACH ROW
BEGIN ATOMIC
WHEN (n.property_id = 175 and not exists (select 1 from
DICT_TOO_TRUTH where dict_value = n.property_value))
THEN RAISE_ERROR('70001', 'Error in property 175')
WHEN (n.property_id = 187 and not exists (select 1 from
DICT_BIOPSY_TRUTH where dict_value = n.property_value))
THEN RAISE_ERROR('70001', 'Error' in property 187)
END;

Notice the error messages are different. How can I do this?

Thanks again for the prompt response!

Alejandrina
Serge Rielau wrote:
Create syntax you have there ... :-)

CREATE TRIGGER fp_insert
NO CASCADE BEFORE INSERT
ON DB2INST1.FILE_PROPERTY
REFERENCING NEW AS n
FOR EACH ROW
WHEN (n.property_id = 175
and not exists (select 1 from DICT_TOO_TRUTH
where dict_value = n.property_value)
OR
n.property_id = 187
and not exists (select 1 from DICT_BIOPSY_TRUTH
where dict_value = n.property_value))
VALUES(RAISE_ERROR('70001', 'Error'));

The trigger condition precedes the trigger body.

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab

WAIUG Conference
http://www.iiug.org/waiug/present/Fo...Forum2006.html
Dec 2 '06 #4
I did not want to use IF because I thought CASE might be faster. Is
this true?

Can you give me an example of how you'd use CASE in my situation?

Alejandrina
Knut Stolze wrote:
apattin wrote:
Hi experts,

I am having a hard time coding a trigger to handle multiple validation
conditions in DB2 UDB V8.1.9.. I thought that a single trigger might be
better than one-trigger-per-condition, but so far, no cigar. Here is
what I'd like to do (pseudo-code):

CREATE TRIGGER fp_insert
NO CASCADE BEFORE INSERT
ON DB2INST1.FILE_PROPERTY
REFERENCING NEW AS n
FOR EACH ROW
BEGIN ATOMIC
WHEN (n.property_id = 175 and not exists (select 1 from
DICT_TOO_TRUTH where dict_value = n.property_value))
THEN RAISE_ERROR('70001', 'Error')
WHEN (n.property_id = 187 and not exists (select 1 from
DICT_BIOPSY_TRUTH where dict_value = n.property_value))
THEN RAISE_ERROR('70001', 'Error')
END;

DB2 does not like multiple "WHEN" conditions and I cannot find the
correct syntax for CASE. Can anyone help?

You could resort to IF ... THEN ... ELSE ... or a CASE statement. With the
latter, you need to put an CASE ... END CASE around the stuff in the atomic
compound.

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Dec 2 '06 #5
apattin wrote:
I did not want to use IF because I thought CASE might be faster. Is
this true?

Can you give me an example of how you'd use CASE in my situation?
case EXPRESSSION is faster than if STATEMENT. Yes

CREATE TRIGGER fp_insert
NO CASCADE BEFORE INSERT
ON DB2INST1.FILE_PROPERTY
REFERENCING NEW AS n
FOR EACH ROW
SELECT CASE WHEN n.property_id = 175
and not exists (select 1 from DICT_TOO_TRUTH
where dict_value = n.property_value)
THEN CAST(raise_error('71001', 'hello') AS INT
WHEN
n.property_id = 187
and not exists (select 1 from DICT_BIOPSY_TRUTH
where dict_value = n.property_value))
RAISE_ERROR('70001', 'World'));
END
FROM SYSIBM.SYSDUMMY1;

raise_error() doesn't have a defined return type, hence the CAST.

Another option would be to use 2 triggers.
Should be the same cost :-)
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab

WAIUG Conference
http://www.iiug.org/waiug/present/Fo...Forum2006.html
Dec 2 '06 #6
apattin wrote:
I did not want to use IF because I thought CASE might be faster. Is
this true?

Can you give me an example of how you'd use CASE in my situation?
case EXPRESSSION is faster than if STATEMENT. Yes

CREATE TRIGGER fp_insert
NO CASCADE BEFORE INSERT
ON DB2INST1.FILE_PROPERTY
REFERENCING NEW AS n
FOR EACH ROW
SELECT CASE WHEN n.property_id = 175
and not exists (select 1 from DICT_TOO_TRUTH
where dict_value = n.property_value)
THEN CAST(raise_error('71001', 'hello') AS INT
WHEN
n.property_id = 187
and not exists (select 1 from DICT_BIOPSY_TRUTH
where dict_value = n.property_value)
THEN RAISE_ERROR('70001', 'World')
END
FROM SYSIBM.SYSDUMMY1;

raise_error() doesn't have a defined return type, hence the CAST.

Another option would be to use 2 triggers.
Should be the same cost :-)

--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab

WAIUG Conference
http://www.iiug.org/waiug/present/Fo...Forum2006.html
Dec 2 '06 #7
Worked like a charm. Thanks for unscrambling the trigger syntax for me!

Serge Rielau wrote:
apattin wrote:
I did not want to use IF because I thought CASE might be faster. Is
this true?

Can you give me an example of how you'd use CASE in my situation?
case EXPRESSSION is faster than if STATEMENT. Yes

CREATE TRIGGER fp_insert
NO CASCADE BEFORE INSERT
ON DB2INST1.FILE_PROPERTY
REFERENCING NEW AS n
FOR EACH ROW
SELECT CASE WHEN n.property_id = 175
and not exists (select 1 from DICT_TOO_TRUTH
where dict_value = n.property_value)
THEN CAST(raise_error('71001', 'hello') AS INT
WHEN
n.property_id = 187
and not exists (select 1 from DICT_BIOPSY_TRUTH
where dict_value = n.property_value))
RAISE_ERROR('70001', 'World'));
END
FROM SYSIBM.SYSDUMMY1;

raise_error() doesn't have a defined return type, hence the CAST.

Another option would be to use 2 triggers.
Should be the same cost :-)
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab

WAIUG Conference
http://www.iiug.org/waiug/present/Fo...Forum2006.html
Dec 5 '06 #8

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

Similar topics

6
by: Rolf Wester | last post by:
Hi, I have a form with a select element with multiple="true". When using the GET method (I suppose the same happens with the POST method) I can seen that the form sends channels=CH1&channels=CH2...
66
by: Darren Dale | last post by:
Hello, def test(data): i = ? This is the line I have trouble with if i==1: return data else: return data a,b,c,d = test()
11
by: Ohaya | last post by:
Hi, I'm trying to understand a situation where ASP seems to be "blocking" of "queuing" requests. This is on a Win2K Advanced Server, with IIS5. I've seen some posts (e.g.,...
6
by: Ben Hallert | last post by:
Hi guys, I'm trying to figure out what bone headed mistake I made on something I put together. I've got a form (named 'context') that has a variable number of select-multiple inputs on it. ...
22
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete...
9
by: Abhishek Srivastava | last post by:
Hello All, In IIS 6.0 We have a concept of worker processes and application pools. As I understand it, we can have multiple worker process per appliction pool. Each worker process is dedicated...
9
by: Graham | last post by:
I have been having some fun learning and using the new Controls and methods in .Net 2.0 which will make my life in the future easier and faster. Specifically the new databinding practises and...
2
by: areef.islam | last post by:
Hi, I am kinda new to javascript and I am having this problem with selecting multiple options from a select tag. Hope someone can help me out here. here is my code...
4
by: Matt Kruse | last post by:
While developing an internal IE6-only webapp, a discussion started about the 'best' way to apply classes to data tables across multiple pages. The two arguments were: 1. Apply a single class to...
35
by: keerthyragavendran | last post by:
hi i'm downloading a single file using multiple threads... how can i specify a particular range of bytes alone from a single large file... for example say if i need only bytes ranging from...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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...
0
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,...

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.