473,387 Members | 1,757 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,387 software developers and data experts.

Adding procedures to triggers...

Need some help understanding how to tie together a trigger and a
procedure together. I want to do a certain procedure if an insert
occurs on TABLE1:

TABLE1 contains COL1, COL2, COL3, COL4

CREATE TRIGGER trigger100 NO CASCADE BEFORE INSERT ON table1
REFERENCING NEW AS newdata FOR EACH ROW MODE DB2ROW
BEGIN
CALL procedure1(?, ?) handle RETURN here??

Then I have the procedure:

CREATE PROCEDURE procedure1(??)
RESULT SET 1
LANGUAGE SQL
BEGIN
....some code.....
IF newdata.COL1 < 7 is THEN
UPDATE an existing record using information from newdata
RETURN 2;
ELSEIF newdata.COL1 = 0 and an existing_record.COL1 = 0 THEN
UPDATE existing record.....
RETURN 1;
ELSEIF newdata.COL1 4 and newdata.COL2 8 THEN
INSERT the original request
RETURN 0;
END

The orignal request was:

INSERT INTO table1 (COL1, COL2, COL3, COL4) VALUES ( 4, 24, 'xxx',
'yyy')

so the questions are:

1) how does the procedure get the data from the original INSERT
statement since it is needed to determine if it should insert or
update a record? can this be passed in as a parameter?

2) How can I get the trigger to handle the various RETURN codes from
the procedure? I could add an IN rc INT to the parameter but I
noticed the RETURN option and was wondering how the caller gets this
info. I want to use the RETURN values to make the trigger SIGNAL an
sqlstate depending on the outcome.

I am sure I will have more questions but that will get me started in
the right direction I hope.

Sep 13 '07 #1
4 1609
shorti wrote:
Need some help understanding how to tie together a trigger and a
procedure together. I want to do a certain procedure if an insert
occurs on TABLE1:

TABLE1 contains COL1, COL2, COL3, COL4

CREATE TRIGGER trigger100 NO CASCADE BEFORE INSERT ON table1
REFERENCING NEW AS newdata FOR EACH ROW MODE DB2ROW
BEGIN
CALL procedure1(?, ?) handle RETURN here??

Then I have the procedure:

CREATE PROCEDURE procedure1(??)
RESULT SET 1
LANGUAGE SQL
BEGIN
....some code.....
IF newdata.COL1 < 7 is THEN
UPDATE an existing record using information from newdata
RETURN 2;
ELSEIF newdata.COL1 = 0 and an existing_record.COL1 = 0 THEN
UPDATE existing record.....
RETURN 1;
ELSEIF newdata.COL1 4 and newdata.COL2 8 THEN
INSERT the original request
RETURN 0;
END

The orignal request was:

INSERT INTO table1 (COL1, COL2, COL3, COL4) VALUES ( 4, 24, 'xxx',
'yyy')

so the questions are:

1) how does the procedure get the data from the original INSERT
statement since it is needed to determine if it should insert or
update a record? can this be passed in as a parameter?
Yes, but you need to pas in each column separately:
CALL procedure1(newdata.col1, newdata.col2, ...)
>
2) How can I get the trigger to handle the various RETURN codes from
the procedure? I could add an IN rc INT to the parameter but I
noticed the RETURN option and was wondering how the caller gets this
info. I want to use the RETURN values to make the trigger SIGNAL an
sqlstate depending on the outcome.
Now there is one I haven't used in while. IIRC it's:
GET DIAGNOSTICS retval = RETURNS_STATUS;
You need to DECLARE retval INTEGER; after the BEGIN ATOMIC

DB2ROW? This is DB2 for iSeries?
>
I am sure I will have more questions but that will get me started in
the right direction I hope.
There are always more questions... ;-)

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Sep 14 '07 #2
>
There are always more questions... ;-)

Cheers
Serge
Ah, yes...you are ever so wise.....because here I am again with yet
another....

A side questions. When you do an OPEN cursor, the cursor is placed
before the record selected by the DECLARE, correct? Can that record
be accessed? In other words, can I SELECT from WHERE CURRENT OF that
cursor even though a FETCH has not been performed?

I want to retrieve data from the record before and after the one
specified in the UPDATE statement by the calling component.

TABLE1
---------------------------------------------
COL1 COL2
ROW1 1 2 <---c1 here upon OPEN
(I want to capture these values)
ROW2 4 8 <---c1 will be here
after FETCH
ROW3 10 12 <---have another cursor
capture record after perhaps

example:

DECLARE c1 CURSOR FOR
SELECT col1, col2 FROM table1 WHERE col1 = 4 AND col2 = 8;
OPEN c1 ; <---opens to record before the one specified
in declare
IF SQLCODE = 0 THEN
SET before_col1 = (SELECT col1 from table1 WHERE CURRENT OF c1)
<------can I do this?
SET before_col2 = (SELECT col2 from table1 WHERE CURRENT OF c1)
<-------can I do this?
FETCH c1 INTO this_col1, this_col2; <-----then move c1 to
the correct record to use later on
......do a bunch of stuff here...

Sep 14 '07 #3
Serge Rielau wrote:
CREATE TRIGGER trg BEFORE UPDATE ON T REFERENCING NEW AS N OLD AS O
FOR EACH ROW
WHEN (EXISTS (SELECT 1 FROM T
WHERE (N.c1 BETWEEN T.c1 AND T.c2
OR N.c2 BETWEEN T.c1 AND T.c2
OR N.c1 <= T.c1 AND N.c2 >= T.c2
)
AND NOT (O.c1 = T.c1 AND O.c2 = T.c2)))
SIGNAL SQLSTATE '78000' SET MESSAGE_TEXT = 'Row range overlaps'
Small correction....
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Sep 15 '07 #4
Thanks Serge,

I believe that would work and much shorter than my prodedure!
Sep 17 '07 #5

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

Similar topics

17
by: serge | last post by:
How can i delete all user stored procedures and all table triggers very fast in a single database? Thank you
5
by: serge | last post by:
What is the best way to run one command and have a database be created and sql scripts run on it to create the tables, indexes, triggers, procedures, etc.? Is there an existing tool free or...
1
by: Manish Bafna | last post by:
speaking of me, I'm not very new to Access, but I haven't worked on it as a professional. I know that you can fire queries in Access, but I don't think you can create views. i want to know that...
5
by: Tim Marshall | last post by:
I was following the thread "Re: Access Treeview - Is it Safe Yet?" with interest and on reading the post describing Lauren Quantrell's SmartTree, I've run into something I don't understand: Stored...
45
by: John | last post by:
Hi When developing vb.bet winform apps bound to sql server datasource, is it preferable to use SELECTs or stored procedure to read and write data from/to SQL Server? Why? Thanks Regards
1
by: SVGK, Raju | last post by:
Hi, I have a table where in lot of triggers were included in that as shown below. How to view/access triggers and procedures from postgresql. I am using postgresql 7.4.1 on solaris. regds...
6
by: aj | last post by:
DB2 LUW 8.1 FP11 Can't I overload SPs based on param types rather than just number of params? If I do: CREATE PROCEDURE FOO( P_DATE_OLD DATE,P_DATE_NEW DATE) then CREATE PROCEDURE FOO(
1
by: Miaaa Mukherjee | last post by:
Hello, I want to know something about the stored procedures and triggers which are used. I want to know that the procedures n triggers r created in SQL Server 2005 or...
5
by: FP | last post by:
Hi, i have read that with Visual Studio 2005 it's possible to create stored procedures and triggers for a Sql Server 2005 database. I must admit that i have already written the stored procedures...
2
by: cvraghavan1979 | last post by:
Hi guys, let me know the difference between the use of direct queries and the stored procedures / Triggers. also pls specify the advantages of using the stored procedures / triggers....
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.