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

Insert and Update Trigger on same Table

I currently have 2 tables as follows:

CREATE TABLE [CRPDTA].[F55MRKT119]
(
mhan8 int,
mhac02 varchar(5),
mhmot varchar(5),
mhupmj int
)

GO

CREATE TABLE [CRPDTA].[F55MRKT11]
(
mdan8 int,
mdac02 varchar(5),
mdmot varchar(5),
mdmail int,
mdmag int,
mdupmj int
)

What I would like to do is place a trigger on F55MRKT119 which will
insert records to the F55MRKT11 if they do not exist in that table
based on the [mdan8] field. If the record does exist I would like
Update the corresponding record and increment either the [MDMAIL] or
the [MDMAG] based on the inserted [MHMOT]. What I have so far is as
follows:

TRIGGER #1:

CREATE TRIGGER trgIns_Summary ON [CRPDTA].[F55MRKT119]
FOR INSERT
AS
BEGIN

INSERT INTO CRPDTA.F55MRKT11
select INS.MHAN8, INS.MHAC02, INS.MHMOT,
case when INS.MHMOT='MAG' then 0 ELSE 1 end,
case when INS.MHMOT='MAG' then 1 ELSE 0 end,
'0' from INSERTED INS
WHERE ins.mhan8 not in(select mdan8 from crpdta.f55MRKT11)

END

TRIGGER #2:

CREATE TRIGGER trgUpd_Summary ON [CRPDTA].[F55MRKT119]
FOR Update
AS
BEGIN

UPDATE CRPDTA.F55MRKT11
SET MDMAIL= case when INS.MHMOT='MAG' then 0+MDMAIL
when INS.MHMOT<>'MAG' then 1+MDMAIL end,
MDMAG= case when INS.MHMOT='MAG' then 1+MDMAG
when INS.MHMOT<>'MAG' then 0+MDMAG end
from INSERTED INS JOIN CRPDTA.F55MRKT11
on(ins.mhan8=mdan8)

END

For instance if I do the following insert:

INSERT INTO CRPDTA.F55MRKT119
VALUES('212131','VK4','AL4','0')

then

INSERT INTO CRPDTA.F55MRKT119
VALUES('212131','VK4','MAG','0')

This is what I expect in both tables:

[CRPDTA.F55MRKT119] (2 Records)

MHAN8 MHAC02 MHMOT MHUPMJ
------ ------ ----- ------
212131 VK4 AL4 0
212131 VK4 MAG 0

[CRPDTA.F55MRKT11] (1 Record)

MDAN8 MDAC02 MDMOT MDMAIL MDMAG MDUPMJ
----- ------ ----- ------ ----- ------
212131 VK4 AL4 1 1 0

The insert part works fine in that it iserts in both tables with the
correct values. However it seems as if the Update protion is failing
for some reason. WHat I have tried so far is setting the trigger order
for the update to run first and vice-versa, but still no luck. Any
help would be appreciated.

Jul 23 '05 #1
1 15380
On 31 Dec 2004 08:19:20 -0800, shottarum wrote:

(snip)
The insert part works fine in that it iserts in both tables with the
correct values. However it seems as if the Update protion is failing
for some reason. WHat I have tried so far is setting the trigger order
for the update to run first and vice-versa, but still no luck. Any
help would be appreciated.


Hi shottarum,

Unless I misunderstand you, you have a misconception about the purpose of
INSERT vs UPDATE triggers. The sole difference between these two is what
fires them, not what they do.

Your post doesn't include the keys of your tables. Your table and column
names are completely non-descriptive as well. This combination makes it
hard to address your post. But I'll give it a try anyway.

If I don't misunderstand you, you want the following:
* For each row inserted in F55MRKT119 with a MHAN8 value that doesn't yet
exist in F55MRKT11, make a copy of that row in F55MRKT11, with the values
for mdmail and mdmag calculated from the new F55MRKT119 row.
* For each row inserted in F55MRKT119 with a MHAN8 value that does already
exist in F55MRKT11, increase mdmail and mdmag, based on the new F55MRKT119
row.

Your post doesn't mention anything about changing F55MRKT11 when rows in
F55MRKT119 are updated or deleted. This means you'll only need an INSERT
trigger, to make all desired changed to F55MRKT11 when row(s) are inserted
in F55MRKT119. You'll need to add the update statement to the insert
statement. It has to go before the insert statement, else the inserted
rows will be updated again in the same trigger execution!

I already said that knowing nothing about your keys makes answering your
question harder. I'll have to make some guesses here. The fact that you
foresee inserts in F55MRKT119 with a MHAN8 that exists in F55MRKT11 (and
is probably based on another row, inserted previously in F55MRKT119)
suggests that MHAN8 is not the primary key for F55MRKT119, but it probably
is for F55MRKT11. This means that both your insert and your update need
some change.

Your insert is flawed because two or more rows with the same value for
MHAN8 might be inserted at once. If that value of MHAN8 is not yet present
in F55MRKT11, your insert statement would insert both new rows in
F55MRKT11, violating the primary key. To prevent this, you'll have to
change the INSERT statement to include aggregation and group by MHAN8. I
have no idea which value you'd like to pick for MDAC02 and MDMOT though.

Your update is flawed because the non-standard UPDATE FROM syntax you used
can have nasty side effects if one row from the table to be updated can be
matched against more than one row from the joined table(s). In your case,
that will happen if two or more rows are inserted with the same MHAN8 that
is already present in F55MRKT11.

I think your trigger has to look something like the below:

CREATE TRIGGER trgIns_Summary ON CRPDTA.F55MRKT119
FOR INSERT
AS
BEGIN
-- Update F55MRKT11 for new F55MRKT119 rows with existing MHAN8 value
UPDATE CRPDTA.F55MRKT11
SET MDMAIL = MDMAIL +
(SELECT COUNT(*)
FROM inserted AS ins
WHERE ins.MHAN8 = F55MRKT11.MDAN8
AND ins.MHMOT <> 'MAG')
,MDMAG = MDMAG +
(SELECT COUNT(*)
FROM inserted AS ins
WHERE ins.MHAN8 = F55MRKT11.MDAN8
AND ins.MHMOT = 'MAG')
WHERE EXISTS (SELECT *
FROM inserted AS ins
WHERE ins.MHAN8 = F55MRKT11.MDAN8)
-- Add rows to F55MRKT11 for new F55MRKT119 rows with new MHAN8 value
INSERT INTO CRPDTA.F55MRKT11
(MDAN8, MDAC02, MDMOT,
MDMAIL, MDMAG, MDUPMJ) -- Always include column list!
SELECT ins.MHAN8, MAX(ins.MHAC02), MAX(ins.MHMOT),
SUM(CASE WHEN ins.MHMOT='MAG' THEN 0 ELSE 1 END),
SUM(CASE WHEN ins.MHMOT='MAG' THEN 1 ELSE 0 END),
'0'
FROM inserted AS ins
WHERE NOT EXISTS (SELECT *
FROM CRDPTA.F55MRKT11 AS f
WHERE f.MDAN8 = ins.MHAN8)
GROUP BY ins.MHAN8
END

The above is untested. Please consult http://www.aspfaq.com/5006 before
posting more questions, as it saves everyone involved (including you!) a
lot of time!

Another advise: using all caps for table and column names generally makes
code harder to read. I'm tempted to say that using meaningless table and
column names makes it hard as well, but I guess that you already know
that, but are forced to because of company policy. Nevertheless, changing
then to more human-understandable names before posting to usenet might be
well worth considering.

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 23 '05 #2

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

Similar topics

4
by: DTB | last post by:
I am having trouble creating an INSTEAD OF trigger in SQL Server to replicate a BEFORE UPDATE trigger from ORACLE. Here is a sample of the ORACLE BEFORE UPDATE trigger: CREATE TRIGGER myTRIGGER ON...
9
by: Martin | last post by:
Hello, I'm new with triggers and I can not find any good example on how to do the following: I have two tables WO and PM with the following fields: WO.WONUM, VARCHAR(10) WO.PMNUM,...
3
by: takilroy | last post by:
Hi, Does anyone know of a simple way to do this? I want to create an insert trigger for a table and if the record already exists based on some criteria, I want to update the table with the...
3
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...
1
by: abhi81 | last post by:
Hello All, I have a table on which I have created a insert,Update and a Delete trigger. All these triggers write a entry to another audit table with the unique key for each table and the timestamp....
1
by: db2admin | last post by:
Hello, I lack knowledge about triggers. I have created trigger on table A Table A ------------------------------------------------- ID INTEGER CASEID ...
1
by: degno84 | last post by:
hi.. i have a problem with this simple trigger! I wont to insert a row in the same table where I update a value. The trigger is that: CREATE OR REPLACE TRIGGER LOG_SPOSTAMENTI_NEW_DEST AFTER...
0
by: magnolia | last post by:
i created a trigger that will record the changes made to a table .everything works fine except the insert query.whenerever i try to insert a record it fires insert and update triger at the same time...
1
by: veasnamuch | last post by:
I have a problem while I create a trigger to my table. My objective is getting any change made to my table and record it in to another table . My have thousands records before I add new trigger to...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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: 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
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...

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.