473,385 Members | 2,044 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,385 software developers and data experts.

INSTEAD OF INSERT ON

Hi,
I need to create a trigger that will trap the insert commands on a
table and if the row already exists, it updates the information.

I started with this exemple but im getting syntax error "An unexpected
token "CREATE TRIGGER Test_INS INSTEAD OF" was found following
"BEGIN-OF-STATEMENT"."
CREATE TRIGGER Test_INS INSTEAD OF INSERT ON v_TESTEXPORTTODB2
REFERENCING NEW AS NewData
FOR EACH ROW MODE DB2SQL
BEGIN ATOMIC

IF exists (select 1 from TESTEXPORTTODB2 where INTTEST =
NewData.INTTEST) then

UPDATE GOHIERBE.TESTEXPORTTODB2
set DTETEST = NewData.DTETEST,
STRTEST = NewData.STRTEST
where INTTEST = NewData.INTTEST;

ELSE

INSERT INTO TESTEXPORTTODB2 VALUES (NewData.INTTEST,
NewData.DTETEST, NewData.STRTEST);

END IF;

END

This is the table:

CREATE TABLE TestExportToDB2
(
intTest INTEGER,
dteTest date,
strTest varchar (50)
)

This is the view:

Create view SELECT * from TestExportToDB2

Any one knows what i am doing wrong?

Thank you for your help

END OF POST
Nov 12 '05 #1
2 4540
Alex wrote:
Hi,
I need to create a trigger that will trap the insert commands on a
table and if the row already exists, it updates the information.

I started with this exemple but im getting syntax error "An unexpected
token "CREATE TRIGGER Test_INS INSTEAD OF" was found following
"BEGIN-OF-STATEMENT"."
CREATE TRIGGER Test_INS INSTEAD OF INSERT ON v_TESTEXPORTTODB2
REFERENCING NEW AS NewData
FOR EACH ROW MODE DB2SQL
BEGIN ATOMIC

IF exists (select 1 from TESTEXPORTTODB2 where INTTEST =
NewData.INTTEST) then

UPDATE GOHIERBE.TESTEXPORTTODB2
set DTETEST = NewData.DTETEST,
STRTEST = NewData.STRTEST
where INTTEST = NewData.INTTEST;

ELSE

INSERT INTO TESTEXPORTTODB2 VALUES (NewData.INTTEST,
NewData.DTETEST, NewData.STRTEST);

END IF;

END


This works fine for me on DB2 V8.1 FP2. Maybe you had some weird special
character at the beginning of your statement, which doesn't show in your
text editor?

--
Knut Stolze
Information Integration
IBM Germany / University of Jena
Nov 12 '05 #2
In DB2 8.1.2 there's a new SQL statement MERGE INTO

From the fine manual:

Example 5: Using dynamically supplied values for an employee row, update
the master employee table if the data corresponds to an existing
employee, or insert the row if the data is for a new employee. The
following example is a fragment of code from a C program.

hv1 =
"MERGE INTO employee AS t
USING TABLE(VALUES(CAST (? AS CHAR(6)), CAST (? AS VARCHAR(12)),
CAST (? AS CHAR(1)), CAST (? AS VARCHAR(15)),
CAST (? AS SMALLINT), CAST (? AS INTEGER)))
s(empno, firstnme, midinit, lastname, edlevel, salary)
ON t.empno = s.empno
WHEN MATCHED THEN
UPDATE SET
salary = s.salary
WHEN NOT MATCHED THEN
INSERT
(empno, firstnme, midinit, lastname, edlevel, salary)
VALUES (s.empno, s.firstnme, s.midinit, s.lastname, s.edlevel,
s.salary)";
EXEC SQL PREPARE s1 FROM :hv1;
EXEC SQL EXECUTE s1 USING '000420', 'SERGE', 'K', 'FIELDING', 18, 39580;

Alex wrote:
Hi,
I need to create a trigger that will trap the insert commands on a
table and if the row already exists, it updates the information.

I started with this exemple but im getting syntax error "An unexpected
token "CREATE TRIGGER Test_INS INSTEAD OF" was found following
"BEGIN-OF-STATEMENT"."
CREATE TRIGGER Test_INS INSTEAD OF INSERT ON v_TESTEXPORTTODB2
REFERENCING NEW AS NewData
FOR EACH ROW MODE DB2SQL
BEGIN ATOMIC

IF exists (select 1 from TESTEXPORTTODB2 where INTTEST =
NewData.INTTEST) then

UPDATE GOHIERBE.TESTEXPORTTODB2
set DTETEST = NewData.DTETEST,
STRTEST = NewData.STRTEST
where INTTEST = NewData.INTTEST;

ELSE

INSERT INTO TESTEXPORTTODB2 VALUES (NewData.INTTEST,
NewData.DTETEST, NewData.STRTEST);

END IF;

END

This is the table:

CREATE TABLE TestExportToDB2
(
intTest INTEGER,
dteTest date,
strTest varchar (50)
)

This is the view:

Create view SELECT * from TestExportToDB2

Any one knows what i am doing wrong?

Thank you for your help

END OF POST

Nov 12 '05 #3

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

Similar topics

6
by: Hareesh | last post by:
Hi, I am trying a Set Approach instead of Using of Cursor (which works). I am attaching the SQL to create tables and the my Procedure, and a piece of code to execute the Procedure. I would...
8
by: joe | last post by:
hi i am trying to write a insted of insert trigger to create a unique id when i insert a record in my database. can anyone give me an example with out using identity. thanks
8
by: Stuart McGraw | last post by:
Is Microsoft full of #*$#*% (again) or am I badly misunderstanding something? Quote from Microsoft's T-SQL doc: > INSTEAD OF triggers are executed instead of the triggering action. > These...
1
by: Alex | last post by:
Hi, I need to create a trigger that will trap the insert commands on a table and if the row already exists, it updates the information. I started with this exemple but im getting syntax...
5
by: William of Ockham | last post by:
Hi, I was asked to recreate a new clean database for our developers because the current one they use is not entirely up to date. So I created a new database and I run into the followin strange...
2
by: leedo | last post by:
Hi, I am almost going crazy with this. I have a table that I bulk insert into from another database using VS2005. I need to change the data in the records before committing the values in the...
1
by: Mike | last post by:
Hi All, I'm using vb.net as my codebehind lang. and the following code is being executed in my aspx.vb page to stamp a DB row. Dim oStatsInfo As New StatsInfo(CartID, Batch, Set, Num, 0, 0, 0,...
0
by: Maryan | last post by:
Hi everybody, i have a view, which contains some columns of two tables. I would like to insert informtions to this view but since this view is created through a join, it is impossible to insert...
1
by: asf93555 | last post by:
Running under SQL2000 I can not get an INSTEAD trigger to function. I've even copied the example directl from books online - no joy . . . Server: Msg 170, Level 15, State 1, Procedure...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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...
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,...

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.